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 (defvar *enclosing-source-cutoff* 1
21 "The maximum number of enclosing non-original source forms (i.e. from
22 macroexpansion) that we print in full. For additional enclosing forms, we
24 (declaim (type unsigned-byte *enclosing-source-cutoff*))
26 ;;; We separate the determination of compiler error contexts from the
27 ;;; actual signalling of those errors by objectifying the error
28 ;;; context. This allows postponement of the determination of how (and
29 ;;; if) to signal the error.
31 ;;; We take care not to reference any of the IR1 so that pending
32 ;;; potential error messages won't prevent the IR1 from being GC'd. To
33 ;;; this end, we convert source forms to strings so that source forms
34 ;;; that contain IR1 references (e.g. %DEFUN) don't hold onto the IR.
35 (defstruct (compiler-error-context
36 #-no-ansi-print-object
37 (:print-object (lambda (x stream)
38 (print-unreadable-object (x stream :type t))))
40 ;; a list of the stringified CARs of the enclosing non-original source forms
41 ;; exceeding the *enclosing-source-cutoff*
42 (enclosing-source nil :type list)
43 ;; a list of stringified enclosing non-original source forms
44 (source nil :type list)
45 ;; the stringified form in the original source that expanded into SOURCE
46 (original-source (missing-arg) :type simple-string)
47 ;; a list of prefixes of "interesting" forms that enclose original-source
48 (context nil :type list)
49 ;; the FILE-INFO-NAME for the relevant FILE-INFO
50 (file-name (missing-arg) :type (or pathname (member :lisp :stream)))
51 ;; the file position at which the top level form starts, if applicable
52 (file-position nil :type (or index null))
53 ;; the original source part of the source path
54 (original-source-path nil :type list)
55 ;; the lexenv active at the time
56 (lexenv nil :type (or null lexenv)))
58 ;;; If true, this is the node which is used as context in compiler warning
60 (declaim (type (or null compiler-error-context node) *compiler-error-context*))
61 (defvar *compiler-error-context* nil)
63 ;;; a hashtable mapping macro names to source context parsers. Each parser
64 ;;; function returns the source-context list for that form.
65 (defvar *source-context-methods* (make-hash-table))
67 ;;; documentation originally from cmu-user.tex:
68 ;;; This macro defines how to extract an abbreviated source context from
69 ;;; the \var{name}d form when it appears in the compiler input.
70 ;;; \var{lambda-list} is a \code{defmacro} style lambda-list used to
71 ;;; parse the arguments. The \var{body} should return a list of
72 ;;; subforms that can be printed on about one line. There are
73 ;;; predefined methods for \code{defstruct}, \code{defmethod}, etc. If
74 ;;; no method is defined, then the first two subforms are returned.
75 ;;; Note that this facility implicitly determines the string name
76 ;;; associated with anonymous functions.
77 ;;; So even though SBCL itself only uses this macro within this file,
78 ;;; it's a reasonable thing to put in SB-EXT in case some dedicated
79 ;;; user wants to do some heavy tweaking to make SBCL give more
80 ;;; informative output about his code.
81 (defmacro define-source-context (name lambda-list &body body)
83 "DEFINE-SOURCE-CONTEXT Name Lambda-List Form*
84 This macro defines how to extract an abbreviated source context from the
85 Named form when it appears in the compiler input. Lambda-List is a DEFMACRO
86 style lambda-list used to parse the arguments. The Body should return a
87 list of subforms suitable for a \"~{~S ~}\" format string."
88 (let ((n-whole (gensym)))
89 `(setf (gethash ',name *source-context-methods*)
91 (destructuring-bind ,lambda-list ,n-whole ,@body)))))
93 (defmacro def-source-context (&rest rest)
94 (deprecation-warning 'def-source-context 'define-source-context)
95 `(define-source-context ,@rest))
97 (define-source-context defstruct (name-or-options &rest slots)
98 (declare (ignore slots))
99 `(defstruct ,(if (consp name-or-options)
100 (car name-or-options)
103 (define-source-context function (thing)
104 (if (and (consp thing) (eq (first thing) 'lambda) (consp (rest thing)))
105 `(lambda ,(second thing))
108 ;;; Return the first two elements of FORM if FORM is a list. Take the
109 ;;; CAR of the second form if appropriate.
110 (defun source-form-context (form)
111 (cond ((atom form) nil)
112 ((>= (length form) 2)
113 (let* ((context-fun-default (lambda (x)
115 (list (first form) (second form))))
116 (context-fun (gethash (first form)
117 *source-context-methods*
118 context-fun-default)))
119 (declare (type function context-fun))
120 (funcall context-fun (rest form))))
124 ;;; Given a source path, return the original source form and a
125 ;;; description of the interesting aspects of the context in which it
126 ;;; appeared. The context is a list of lists, one sublist per context
127 ;;; form. The sublist is a list of some of the initial subforms of the
130 ;;; For now, we use the first two subforms of each interesting form. A
131 ;;; form is interesting if the first element is a symbol beginning
132 ;;; with "DEF" and it is not the source form. If there is no
133 ;;; DEF-mumble, then we use the outermost containing form. If the
134 ;;; second subform is a list, then in some cases we return the CAR of
135 ;;; that form rather than the whole form (i.e. don't show DEFSTRUCT
137 (defun find-original-source (path)
138 (declare (list path))
139 (let* ((rpath (reverse (source-path-original-source path)))
141 (root (find-source-root tlf *source-info*)))
144 (current (rest rpath)))
147 (aver (null current))
149 (let ((head (first form)))
151 (let ((name (symbol-name head)))
152 (when (and (>= (length name) 3) (string= name "DEF" :end1 3))
153 (context (source-form-context form))))))
154 (when (null current) (return))
155 (setq form (nth (pop current) form)))
158 (values form (context)))
160 (let ((c (source-form-context root)))
161 (values form (if c (list c) nil))))
163 (values '(unable to locate source)
164 '((some strange place)))))))))
166 ;;; Convert a source form to a string, suitably formatted for use in
167 ;;; compiler warnings.
168 (defun stringify-form (form &optional (pretty t))
169 (with-standard-io-syntax
170 (with-compiler-io-syntax
171 (let ((*print-pretty* pretty))
173 (format nil "~<~@; ~S~:>" (list form))
174 (prin1-to-string form))))))
176 ;;; Return a COMPILER-ERROR-CONTEXT structure describing the current
177 ;;; error context, or NIL if we can't figure anything out. ARGS is a
178 ;;; list of things that are going to be printed out in the error
179 ;;; message, and can thus be blown off when they appear in the source
181 (defun find-error-context (args)
182 (let ((context *compiler-error-context*))
183 (if (compiler-error-context-p context)
185 (let ((path (or (and (boundp '*current-path*) *current-path*)
187 (node-source-path context)
189 (when (and *source-info* path)
190 (multiple-value-bind (form src-context) (find-original-source path)
191 (collect ((full nil cons)
193 (let ((forms (source-path-forms path))
195 (dolist (src (if (member (first forms) args)
198 (if (>= n *enclosing-source-cutoff*)
199 (short (stringify-form (if (consp src)
203 (full (stringify-form src)))
206 (let* ((tlf (source-path-tlf-number path))
207 (file-info (source-info-file-info *source-info*)))
208 (make-compiler-error-context
209 :enclosing-source (short)
211 :original-source (stringify-form form)
213 :file-name (file-info-name file-info)
215 (multiple-value-bind (ignore pos)
216 (find-source-root tlf *source-info*)
217 (declare (ignore ignore))
219 :original-source-path
220 (source-path-original-source path)
222 (node-lexenv context)
223 (if (boundp '*lexenv*) *lexenv* nil)))))))))))
225 ;;;; printing error messages
227 ;;; We save the context information that we printed out most recently
228 ;;; so that we don't print it out redundantly.
230 ;;; The last COMPILER-ERROR-CONTEXT that we printed.
231 (defvar *last-error-context* nil)
232 (declaim (type (or compiler-error-context null) *last-error-context*))
234 ;;; The format string and args for the last error we printed.
235 (defvar *last-format-string* nil)
236 (defvar *last-format-args* nil)
237 (declaim (type (or string null) *last-format-string*))
238 (declaim (type list *last-format-args*))
240 ;;; The number of times that the last error message has been emitted,
241 ;;; so that we can compress duplicate error messages.
242 (defvar *last-message-count* 0)
243 (declaim (type index *last-message-count*))
245 ;;; If the last message was given more than once, then print out an
246 ;;; indication of how many times it was repeated. We reset the message
247 ;;; count when we are done.
248 (defun note-message-repeats (stream &optional (terpri t))
249 (cond ((= *last-message-count* 1)
252 ((> *last-message-count* 1)
253 (format stream "~&; [Last message occurs ~W times.]~2%"
254 *last-message-count*)))
255 (setq *last-message-count* 0))
257 ;;; Print out the message, with appropriate context if we can find it.
258 ;;; If the context is different from the context of the last message
259 ;;; we printed, then we print the context. If the original source is
260 ;;; different from the source we are working on, then we print the
261 ;;; current source in addition to the original source.
263 ;;; We suppress printing of messages identical to the previous, but
264 ;;; record the number of times that the message is repeated.
265 (defmacro print-compiler-message (stream format-string format-args)
266 `(with-compiler-io-syntax
267 (%print-compiler-message ,stream ,format-string ,format-args)))
269 (defun %print-compiler-message (stream format-string format-args)
270 (declare (type simple-string format-string))
271 (declare (type list format-args))
272 (let ((context (find-error-context format-args)))
274 (let ((file (compiler-error-context-file-name context))
275 (in (compiler-error-context-context context))
276 (form (compiler-error-context-original-source context))
277 (enclosing (compiler-error-context-enclosing-source context))
278 (source (compiler-error-context-source context))
279 (last *last-error-context*))
282 (equal file (compiler-error-context-file-name last)))
283 (when (pathnamep file)
284 (note-message-repeats stream)
286 (format stream "~2&; file: ~A~%" (namestring file))))
289 (equal in (compiler-error-context-context last)))
290 (note-message-repeats stream)
292 (pprint-logical-block (stream nil :per-line-prefix "; ")
293 (format stream "in:~{~<~% ~4:;~{ ~S~}~>~^ =>~}" in))
298 (compiler-error-context-original-source last)))
299 (note-message-repeats stream)
301 (pprint-logical-block (stream nil :per-line-prefix "; ")
302 (format stream " ~A" form))
307 (compiler-error-context-enclosing-source last)))
309 (note-message-repeats stream)
311 (format stream "~&; --> ~{~<~%; --> ~1:;~A~> ~}~%" enclosing)))
314 (equal source (compiler-error-context-source last)))
315 (setq *last-format-string* nil)
317 (note-message-repeats stream)
320 (write-string "; ==>" stream)
322 (pprint-logical-block (stream nil :per-line-prefix "; ")
323 (write-string src stream)))))))
326 (note-message-repeats stream)
327 (setq *last-format-string* nil)))
329 (setq *last-error-context* context))
331 ;; FIXME: this testing for effective equality of compiler messages
332 ;; is ugly, and really ought to be done at a higher level.
333 (unless (and (equal format-string *last-format-string*)
334 (tree-equal format-args *last-format-args*))
335 (note-message-repeats stream nil)
336 (setq *last-format-string* format-string)
337 (setq *last-format-args* format-args)
339 (pprint-logical-block (stream nil :per-line-prefix "; ")
340 (format stream "~&~?" format-string format-args))
343 (incf *last-message-count*)
346 (defun print-compiler-condition (condition)
347 (declare (type condition condition))
348 (let (;; These different classes of conditions have different
349 ;; effects on the return codes of COMPILE-FILE, so it's nice
350 ;; for users to be able to pick them out by lexical search
351 ;; through the output.
352 (what (etypecase condition
353 (style-warning 'style-warning)
355 ((or error compiler-error) 'error))))
356 (print-compiler-message
358 (format nil "caught ~S:~%~~@< ~~@;~~A~~:>" what)
359 (list (princ-to-string condition)))))
361 ;;; The act of signalling one of these beasts must not cause WARNINGSP
362 ;;; (or FAILUREP) to be set from COMPILE or COMPILE-FILE, so we can't
363 ;;; inherit from WARNING or STYLE-WARNING.
365 ;;; FIXME: the handling of compiler-notes could be unified with
366 ;;; warnings and style-warnings (see the various handler functions
368 (define-condition compiler-note (condition) ()
370 "Root of the hierarchy of conditions representing information discovered
371 by the compiler that the user might wish to know, but which does not merit
372 a STYLE-WARNING (or any more serious condition)."))
373 (define-condition simple-compiler-note (simple-condition compiler-note) ())
374 (define-condition code-deletion-note (simple-compiler-note) ()
376 "A condition type signalled when the compiler deletes code that the user
377 has written, having proved that it is unreachable."))
379 (macrolet ((with-condition ((condition datum args) &body body)
380 (with-unique-names (block)
383 (coerce-to-condition ,datum ,args
384 'simple-compiler-note
389 (return-from ,block (values))))
393 (defun compiler-notify (datum &rest args)
394 (unless (if *compiler-error-context*
395 (policy *compiler-error-context* (= inhibit-warnings 3))
396 (policy *lexenv* (= inhibit-warnings 3)))
397 (with-condition (condition datum args)
398 (incf *compiler-note-count*)
399 (print-compiler-message
401 (format nil "note: ~~A")
402 (list (princ-to-string condition)))))
405 ;; Issue a note when we might or might not be in the compiler.
406 (defun maybe-compiler-notify (&rest rest)
407 (if (boundp '*lexenv*) ; if we're in the compiler
408 (apply #'compiler-notify rest)
409 (with-condition (condition (car rest) (cdr rest))
410 (let ((stream *error-output*))
411 (pprint-logical-block (stream nil :per-line-prefix ";")
412 (format stream " note: ~3I~_")
413 (pprint-logical-block (stream nil)
414 (format stream "~A" condition)))
415 ;; (outside logical block, no per-line-prefix)
416 (fresh-line stream))))))
418 ;;; The politically correct way to print out progress messages and
419 ;;; such like. We clear the current error context so that we know that
420 ;;; it needs to be reprinted, and we also FORCE-OUTPUT so that the
421 ;;; message gets seen right away.
422 (declaim (ftype (function (string &rest t) (values)) compiler-mumble))
423 (defun compiler-mumble (control &rest args)
424 (let ((stream *standard-output*))
425 (note-message-repeats stream)
426 (setq *last-error-context* nil)
427 (apply #'format stream control args)
428 (force-output stream)
431 ;;; Return a string that somehow names the code in COMPONENT. We use
432 ;;; the source path for the bind node for an arbitrary entry point to
433 ;;; find the source context, then return that as a string.
434 (declaim (ftype (function (component) simple-string) find-component-name))
435 (defun find-component-name (component)
436 (let ((ep (first (block-succ (component-head component)))))
437 (aver ep) ; else no entry points??
438 (multiple-value-bind (form context)
439 (find-original-source
440 (node-source-path (block-start-node ep)))
441 (declare (ignore form))
442 (let ((*print-level* 2)
443 (*print-pretty* nil))
444 (format nil "~{~{~S~^ ~}~^ => ~}" context)))))
446 ;;;; condition system interface
448 ;;; Keep track of how many times each kind of condition happens.
449 (defvar *compiler-error-count*)
450 (defvar *compiler-warning-count*)
451 (defvar *compiler-style-warning-count*)
452 (defvar *compiler-note-count*)
454 ;;; Keep track of whether any surrounding COMPILE or COMPILE-FILE call
455 ;;; should return WARNINGS-P or FAILURE-P.
457 (defvar *warnings-p*)
459 ;;; condition handlers established by the compiler. We re-signal the
460 ;;; condition, then if it isn't handled, we increment our warning
461 ;;; counter and print the error message.
462 (defun compiler-error-handler (condition)
464 (incf *compiler-error-count*)
467 (print-compiler-condition condition)
468 (continue condition))
469 (defun compiler-warning-handler (condition)
471 (incf *compiler-warning-count*)
474 (print-compiler-condition condition)
475 (muffle-warning condition))
476 (defun compiler-style-warning-handler (condition)
478 (incf *compiler-style-warning-count*)
479 (setf *warnings-p* t)
480 (print-compiler-condition condition)
481 (muffle-warning condition))
483 ;;;; undefined warnings
485 (defvar *undefined-warning-limit* 3
487 "If non-null, then an upper limit on the number of unknown function or type
488 warnings that the compiler will print for any given name in a single
489 compilation. This prevents excessive amounts of output when the real
490 problem is a missing definition (as opposed to a typo in the use.)")
492 ;;; Make an entry in the *UNDEFINED-WARNINGS* describing a reference
493 ;;; to NAME of the specified KIND. If we have exceeded the warning
494 ;;; limit, then just increment the count, otherwise note the current
497 ;;; Undefined types are noted by a condition handler in
498 ;;; WITH-COMPILATION-UNIT, which can potentially be invoked outside
499 ;;; the compiler, hence the BOUNDP check.
500 (defun note-undefined-reference (name kind)
502 ;; Check for boundness so we don't blow up if we're called
503 ;; when IR1 conversion isn't going on.
506 ;; FIXME: I'm pretty sure the INHIBIT-WARNINGS test below
507 ;; isn't a good idea; we should have INHIBIT-WARNINGS
508 ;; affect compiler notes, not STYLE-WARNINGs. And I'm not
509 ;; sure what the BOUNDP '*LEXENV* test above is for; it's
510 ;; likely a good idea, but it probably deserves an
511 ;; explanatory comment.
512 (policy *lexenv* (= inhibit-warnings 3))
513 ;; KLUDGE: weird decoupling between here and where we're
514 ;; going to signal the condition. I don't think we can
515 ;; rewrite this using SIGNAL and RESTART-CASE (to take
516 ;; advantage of the (SATISFIES HANDLE-CONDITION-P)
517 ;; handler, because if that doesn't handle it the ordinary
518 ;; compiler handlers will trigger.
521 (:variable (make-condition 'warning))
522 ((:function :type) (make-condition 'style-warning)))
524 (rassoc 'muffle-warning
525 (lexenv-handled-conditions *lexenv*))))))
526 (let* ((found (dolist (warning *undefined-warnings* nil)
527 (when (and (equal (undefined-warning-name warning) name)
528 (eq (undefined-warning-kind warning) kind))
531 (make-undefined-warning :name name :kind kind))))
532 (unless found (push res *undefined-warnings*))
533 (when (or (not *undefined-warning-limit*)
534 (< (undefined-warning-count res) *undefined-warning-limit*))
535 (push (find-error-context (list name))
536 (undefined-warning-warnings res)))
537 (incf (undefined-warning-count res))))