6143aeeaf34dcaea0326275fdefd7105473c8bab
[sbcl.git] / src / compiler / ir1report.lisp
1 ;;;; machinery for reporting errors/warnings/notes/whatnot from
2 ;;;; the compiler
3
4 ;;;; This software is part of the SBCL system. See the README file for
5 ;;;; more information.
6 ;;;;
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.
12
13 (in-package "SB!C")
14 \f
15 ;;;; compiler error context determination
16
17 (declaim (special *current-path*))
18
19 ;;; We bind print level and length when printing out messages so that
20 ;;; we don't dump huge amounts of garbage.
21 ;;;
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
31   #!+sb-doc
32   "the value for *PRINT-LEVEL* when printing compiler error messages")
33 (defvar *compiler-error-print-length* 10
34   #!+sb-doc
35   "the value for *PRINT-LENGTH* when printing compiler error messages")
36 (defvar *compiler-error-print-lines* 12
37   #!+sb-doc
38   "the value for *PRINT-LINES* when printing compiler error messages")
39
40 (defvar *enclosing-source-cutoff* 1
41   #!+sb-doc
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
44   print only the CAR.")
45 (declaim (type unsigned-byte *enclosing-source-cutoff*))
46
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.
51 ;;;
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))))
60             (:copier nil))
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))
76
77 ;;; If true, this is the node which is used as context in compiler warning
78 ;;; messages.
79 (declaim (type (or null compiler-error-context node) *compiler-error-context*))
80 (defvar *compiler-error-context* nil)
81
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))
85
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 def-source-context (name lambda-list &body body)
101   #!+sb-doc
102   "DEF-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*)
109            #'(lambda (,n-whole)
110                (destructuring-bind ,lambda-list ,n-whole ,@body)))))
111
112 (def-source-context defstruct (name-or-options &rest slots)
113   (declare (ignore slots))
114   `(defstruct ,(if (consp name-or-options)
115                    (car name-or-options)
116                    name-or-options)))
117
118 (def-source-context function (thing)
119   (if (and (consp thing) (eq (first thing) 'lambda) (consp (rest thing)))
120       `(lambda ,(second thing))
121       `(function ,thing)))
122
123 ;;; Return the first two elements of FORM if FORM is a list. Take the
124 ;;; CAR of the second form if appropriate.
125 (defun source-form-context (form)
126   (cond ((atom form) nil)
127         ((>= (length form) 2)
128          (funcall (gethash (first form) *source-context-methods*
129                            #'(lambda (x)
130                                (declare (ignore x))
131                                (list (first form) (second form))))
132                   (rest form)))
133         (t
134          form)))
135
136 ;;; Given a source path, return the original source form and a
137 ;;; description of the interesting aspects of the context in which it
138 ;;; appeared. The context is a list of lists, one sublist per context
139 ;;; form. The sublist is a list of some of the initial subforms of the
140 ;;; context form.
141 ;;;
142 ;;; For now, we use the first two subforms of each interesting form. A
143 ;;; form is interesting if the first element is a symbol beginning
144 ;;; with "DEF" and it is not the source form. If there is no
145 ;;; DEF-mumble, then we use the outermost containing form. If the
146 ;;; second subform is a list, then in some cases we return the CAR of
147 ;;; that form rather than the whole form (i.e. don't show DEFSTRUCT
148 ;;; options, etc.)
149 (defun find-original-source (path)
150   (declare (list path))
151   (let* ((rpath (reverse (source-path-original-source path)))
152          (tlf (first rpath))
153          (root (find-source-root tlf *source-info*)))
154     (collect ((context))
155       (let ((form root)
156             (current (rest rpath)))
157         (loop
158           (when (atom form)
159             (aver (null current))
160             (return))
161           (let ((head (first form)))
162             (when (symbolp head)
163               (let ((name (symbol-name head)))
164                 (when (and (>= (length name) 3) (string= name "DEF" :end1 3))
165                   (context (source-form-context form))))))
166           (when (null current) (return))
167           (setq form (nth (pop current) form)))
168         
169         (cond ((context)
170                (values form (context)))
171               ((and path root)
172                (let ((c (source-form-context root)))
173                  (values form (if c (list c) nil))))
174               (t
175                (values '(unable to locate source)
176                        '((some strange place)))))))))
177
178 ;;; Convert a source form to a string, suitably formatted for use in
179 ;;; compiler warnings.
180 (defun stringify-form (form &optional (pretty t))
181   (with-standard-io-syntax
182    (let ((*print-readably* nil)
183          (*print-pretty* pretty)
184          (*print-level* *compiler-error-print-level*)
185          (*print-length* *compiler-error-print-length*)
186          (*print-lines* *compiler-error-print-lines*))
187      (if pretty
188          (format nil "~<~@;  ~S~:>" (list form))
189          (prin1-to-string form)))))
190
191 ;;; shorthand for creating debug names from source names or other
192 ;;; stems, e.g.
193 ;;;   (DEBUG-NAMIFY "FLET ~S" SOURCE-NAME)
194 ;;;   (DEBUG-NAMIFY "top level form ~S" FORM)
195 ;;;
196 ;;; FIXME: This function seems to have a lot in common with
197 ;;; STRINGIFY-FORM, and perhaps there's some way to merge the two
198 ;;; functions.
199 (defun debug-namify (format-string &rest format-arguments)
200   (with-standard-io-syntax
201    (let ((*print-readably* nil)
202          (*package* *cl-package*)
203          (*print-length* 3)
204          (*print-level* 2))
205      (apply #'format nil format-string format-arguments))))
206
207 ;;; Return a COMPILER-ERROR-CONTEXT structure describing the current
208 ;;; error context, or NIL if we can't figure anything out. ARGS is a
209 ;;; list of things that are going to be printed out in the error
210 ;;; message, and can thus be blown off when they appear in the source
211 ;;; context.
212 (defun find-error-context (args)
213   (let ((context *compiler-error-context*))
214     (if (compiler-error-context-p context)
215         context
216         (let ((path (or (and (boundp '*current-path*) *current-path*)
217                         (if context
218                             (node-source-path context)
219                             nil))))
220           (when (and *source-info* path)
221             (multiple-value-bind (form src-context) (find-original-source path)
222               (collect ((full nil cons)
223                         (short nil cons))
224                 (let ((forms (source-path-forms path))
225                       (n 0))
226                   (dolist (src (if (member (first forms) args)
227                                    (rest forms)
228                                    forms))
229                     (if (>= n *enclosing-source-cutoff*)
230                         (short (stringify-form (if (consp src)
231                                                    (car src)
232                                                    src)
233                                                nil))
234                         (full (stringify-form src)))
235                     (incf n)))
236
237                 (let* ((tlf (source-path-tlf-number path))
238                        (file-info (source-info-file-info *source-info*)))
239                   (make-compiler-error-context
240                    :enclosing-source (short)
241                    :source (full)
242                    :original-source (stringify-form form)
243                    :context src-context
244                    :file-name (file-info-name file-info)
245                    :file-position
246                    (multiple-value-bind (ignore pos)
247                        (find-source-root tlf *source-info*)
248                      (declare (ignore ignore))
249                      pos)
250                    :original-source-path
251                    (source-path-original-source path))))))))))
252 \f
253 ;;;; printing error messages
254
255 ;;; We save the context information that we printed out most recently
256 ;;; so that we don't print it out redundantly.
257
258 ;;; The last COMPILER-ERROR-CONTEXT that we printed.
259 (defvar *last-error-context* nil)
260 (declaim (type (or compiler-error-context null) *last-error-context*))
261
262 ;;; The format string and args for the last error we printed.
263 (defvar *last-format-string* nil)
264 (defvar *last-format-args* nil)
265 (declaim (type (or string null) *last-format-string*))
266 (declaim (type list *last-format-args*))
267
268 ;;; The number of times that the last error message has been emitted,
269 ;;; so that we can compress duplicate error messages.
270 (defvar *last-message-count* 0)
271 (declaim (type index *last-message-count*))
272
273 ;;; If the last message was given more than once, then print out an
274 ;;; indication of how many times it was repeated. We reset the message
275 ;;; count when we are done.
276 (defun note-message-repeats (&optional (terpri t))
277   (cond ((= *last-message-count* 1)
278          (when terpri (terpri *error-output*)))
279         ((> *last-message-count* 1)
280           (format *error-output* "~&; [Last message occurs ~W times.]~2%"
281                  *last-message-count*)))
282   (setq *last-message-count* 0))
283
284 ;;; Print out the message, with appropriate context if we can find it.
285 ;;; If the context is different from the context of the last message
286 ;;; we printed, then we print the context. If the original source is
287 ;;; different from the source we are working on, then we print the
288 ;;; current source in addition to the original source.
289 ;;;
290 ;;; We suppress printing of messages identical to the previous, but
291 ;;; record the number of times that the message is repeated.
292 (defun print-compiler-message (format-string format-args)
293
294   (declare (type simple-string format-string))
295   (declare (type list format-args))
296   
297   (let ((stream *error-output*)
298         (context (find-error-context format-args)))
299     (cond
300      (context
301       (let ((file (compiler-error-context-file-name context))
302             (in (compiler-error-context-context context))
303             (form (compiler-error-context-original-source context))
304             (enclosing (compiler-error-context-enclosing-source context))
305             (source (compiler-error-context-source context))
306             (last *last-error-context*))
307
308         (unless (and last
309                      (equal file (compiler-error-context-file-name last)))
310           (when (pathnamep file)
311             (note-message-repeats)
312             (setq last nil)
313             (format stream "~2&; file: ~A~%" (namestring file))))
314
315         (unless (and last
316                      (equal in (compiler-error-context-context last)))
317           (note-message-repeats)
318           (setq last nil)
319           (format stream "~&")
320           (pprint-logical-block (stream nil :per-line-prefix "; ")
321             (format stream "in:~{~<~%    ~4:;~{ ~S~}~>~^ =>~}" in))
322           (format stream "~%"))
323
324
325         (unless (and last
326                      (string= form
327                               (compiler-error-context-original-source last)))
328           (note-message-repeats)
329           (setq last nil)
330           (format stream "~&")
331           (pprint-logical-block (stream nil :per-line-prefix "; ")
332             (format stream "  ~A" form))
333           (format stream "~&"))
334
335         (unless (and last
336                      (equal enclosing
337                             (compiler-error-context-enclosing-source last)))
338           (when enclosing
339             (note-message-repeats)
340             (setq last nil)
341             (format stream "~&; --> ~{~<~%; --> ~1:;~A~> ~}~%" enclosing)))
342
343         (unless (and last
344                      (equal source (compiler-error-context-source last)))
345           (setq *last-format-string* nil)
346           (when source
347             (note-message-repeats)
348             (dolist (src source)
349               (format stream "~&")
350               (write-string "; ==>" stream)
351               (format stream "~&")
352               (pprint-logical-block (stream nil :per-line-prefix "; ")
353                 (write-string src stream)))))))
354      (t
355        (format stream "~&")
356       (note-message-repeats)
357       (setq *last-format-string* nil)
358        (format stream "~&")))
359
360     (setq *last-error-context* context)
361
362     (unless (and (equal format-string *last-format-string*)
363                  (tree-equal format-args *last-format-args*))
364       (note-message-repeats nil)
365       (setq *last-format-string* format-string)
366       (setq *last-format-args* format-args)
367       (let ((*print-level*  *compiler-error-print-level*)
368             (*print-length* *compiler-error-print-length*)
369             (*print-lines*  *compiler-error-print-lines*))
370         (format stream "~&")
371         (pprint-logical-block (stream nil :per-line-prefix "; ")
372           (format stream "~&~?" format-string format-args))
373         (format stream "~&"))))
374
375   (incf *last-message-count*)
376   (values))
377
378 (defun print-compiler-condition (condition)
379   (declare (type condition condition))
380   (let (;; These different classes of conditions have different
381         ;; effects on the return codes of COMPILE-FILE, so it's nice
382         ;; for users to be able to pick them out by lexical search
383         ;; through the output.
384         (what (etypecase condition
385                 (style-warning 'style-warning)
386                 (warning 'warning)
387                 (error 'error))))
388     (multiple-value-bind (format-string format-args)
389         (if (typep condition 'simple-condition)
390             (values (simple-condition-format-control condition)
391                     (simple-condition-format-arguments condition))
392             (values "~A"
393                     (list (with-output-to-string (s)
394                             (princ condition s)))))
395       (print-compiler-message (format nil
396                                       "caught ~S:~%  ~A"
397                                       what
398                                       format-string)
399                               format-args)))
400   (values))
401
402 ;;; COMPILER-NOTE is vaguely like COMPILER-ERROR and the other
403 ;;; condition-signalling functions, but it just writes some output
404 ;;; instead of signalling. (In CMU CL, it did signal a condition, but
405 ;;; this didn't seem to work all that well; it was weird to have
406 ;;; COMPILE-FILE return with WARNINGS-P set when the only problem was
407 ;;; that the compiler couldn't figure out how to compile something as
408 ;;; efficiently as it liked.)
409 (defun compiler-note (format-string &rest format-args)
410   (unless (if *compiler-error-context*
411               (policy *compiler-error-context* (= inhibit-warnings 3))
412               (policy *lexenv* (= inhibit-warnings 3)))
413     (incf *compiler-note-count*)
414     (print-compiler-message (format nil "note: ~A" format-string)
415                             format-args))
416   (values))
417
418 ;;; Issue a note when we might or might not be in the compiler.
419 (defun maybe-compiler-note (&rest rest)
420   (if (boundp '*lexenv*) ; if we're in the compiler
421       (apply #'compiler-note rest)
422       (let ((stream *error-output*))
423         (pprint-logical-block (stream nil :per-line-prefix ";")
424           
425           (format stream " note: ~3I~_")
426           (pprint-logical-block (stream nil)
427             (apply #'format stream rest)))
428         (fresh-line stream)))) ; (outside logical block, no per-line-prefix)
429
430 ;;; The politically correct way to print out progress messages and
431 ;;; such like. We clear the current error context so that we know that
432 ;;; it needs to be reprinted, and we also FORCE-OUTPUT so that the
433 ;;; message gets seen right away.
434 (declaim (ftype (function (string &rest t) (values)) compiler-mumble))
435 (defun compiler-mumble (format-string &rest format-args)
436   (note-message-repeats)
437   (setq *last-error-context* nil)
438   (apply #'format *error-output* format-string format-args)
439   (force-output *error-output*)
440   (values))
441
442 ;;; Return a string that somehow names the code in COMPONENT. We use
443 ;;; the source path for the bind node for an arbitrary entry point to
444 ;;; find the source context, then return that as a string.
445 (declaim (ftype (function (component) simple-string) find-component-name))
446 (defun find-component-name (component)
447   (let ((ep (first (block-succ (component-head component)))))
448     (aver ep) ; else no entry points??
449     (multiple-value-bind (form context)
450         (find-original-source
451          (node-source-path (continuation-next (block-start ep))))
452       (declare (ignore form))
453       (let ((*print-level* 2)
454             (*print-pretty* nil))
455         (format nil "~{~{~S~^ ~}~^ => ~}" context)))))
456 \f
457 ;;;; condition system interface
458
459 ;;; Keep track of how many times each kind of condition happens.
460 (defvar *compiler-error-count*)
461 (defvar *compiler-warning-count*)
462 (defvar *compiler-style-warning-count*)
463 (defvar *compiler-note-count*)
464
465 ;;; Keep track of whether any surrounding COMPILE or COMPILE-FILE call
466 ;;; should return WARNINGS-P or FAILURE-P.
467 (defvar *failure-p*)
468 (defvar *warnings-p*)
469
470 ;;; condition handlers established by the compiler. We re-signal the
471 ;;; condition, then if it isn't handled, we increment our warning
472 ;;; counter and print the error message.
473 (defun compiler-error-handler (condition)
474   (signal condition)
475   (incf *compiler-error-count*)
476   (setf *warnings-p* t
477         *failure-p* t)
478   (print-compiler-condition condition)
479   (continue condition))
480 (defun compiler-warning-handler (condition)
481   (signal condition)
482   (incf *compiler-warning-count*)
483   (setf *warnings-p* t
484         *failure-p* t)
485   (print-compiler-condition condition)
486   (muffle-warning condition))
487 (defun compiler-style-warning-handler (condition)
488   (signal condition)
489   (incf *compiler-style-warning-count*)
490   (setf *warnings-p* t)
491   (print-compiler-condition condition)
492   (muffle-warning condition))
493 \f
494 ;;;; undefined warnings
495
496 (defvar *undefined-warning-limit* 3
497   #!+sb-doc
498   "If non-null, then an upper limit on the number of unknown function or type
499   warnings that the compiler will print for any given name in a single
500   compilation. This prevents excessive amounts of output when the real
501   problem is a missing definition (as opposed to a typo in the use.)")
502
503 ;;; Make an entry in the *UNDEFINED-WARNINGS* describing a reference
504 ;;; to NAME of the specified KIND. If we have exceeded the warning
505 ;;; limit, then just increment the count, otherwise note the current
506 ;;; error context.
507 ;;;
508 ;;; Undefined types are noted by a condition handler in
509 ;;; WITH-COMPILATION-UNIT, which can potentially be invoked outside
510 ;;; the compiler, hence the BOUNDP check.
511 (defun note-undefined-reference (name kind)
512   (unless (and
513            ;; Check for boundness so we don't blow up if we're called
514            ;; when IR1 conversion isn't going on.
515            (boundp '*lexenv*)
516            ;; FIXME: I'm pretty sure the INHIBIT-WARNINGS test below
517            ;; isn't a good idea; we should have INHIBIT-WARNINGS
518            ;; affect compiler notes, not STYLE-WARNINGs. And I'm not
519            ;; sure what the BOUNDP '*LEXENV* test above is for; it's
520            ;; likely a good idea, but it probably deserves an
521            ;; explanatory comment.
522            (policy *lexenv* (= inhibit-warnings 3)))
523     (let* ((found (dolist (warning *undefined-warnings* nil)
524                     (when (and (equal (undefined-warning-name warning) name)
525                                (eq (undefined-warning-kind warning) kind))
526                       (return warning))))
527            (res (or found
528                     (make-undefined-warning :name name :kind kind))))
529       (unless found (push res *undefined-warnings*))
530       (when (or (not *undefined-warning-limit*)
531                 (< (undefined-warning-count res) *undefined-warning-limit*))
532         (push (find-error-context (list name))
533               (undefined-warning-warnings res)))
534       (incf (undefined-warning-count res))))
535   (values))