| 1 |
|
| 2 |
;;;; Copyright (c) 1984, Taiichi Yuasa and Masami Hagiya |
| 3 |
;;;; Copyright (c) 1990, Giuseppe Attardi |
| 4 |
;;;; Copyright (c) 2003, Juan Jose Garcia-Ripoll |
| 5 |
;;;; Copyright (c) 2023, Daniel Kochmański |
| 6 |
;;;; |
| 7 |
;;;; See file 'LICENSE' for the copyright details. |
| 8 |
|
| 9 |
(in-package #:compiler) |
| 10 |
|
| 11 |
#+cmu-format |
| 12 |
(progn |
| 13 |
(defconstant +note-format+ "~&~@< ~;~?~;~:@>") |
| 14 |
(defconstant +warn-format+ "~&~@< ! ~;~?~;~:@>") |
| 15 |
(defconstant +error-format+ "~&~@< * ~;~?~;~:@>") |
| 16 |
(defconstant +fatal-format+ "~&~@< ** ~;~?~;~:@>")) |
| 17 |
#-cmu-format |
| 18 |
(progn |
| 19 |
(defconstant +note-format+ "~& ~?") |
| 20 |
(defconstant +warn-format+ "~& ! ~?") |
| 21 |
(defconstant +error-format+ "~& * ~?") |
| 22 |
(defconstant +fatal-format+ "~& ** ~?")) |
| 23 |
|
| 24 |
;; For indirect use in :REPORT functions |
| 25 |
(defun compiler-message-report (stream c format-control &rest format-arguments) |
| 26 |
(let ((position (compiler-message-file-position c)) |
| 27 |
(prefix (compiler-message-prefix c)) |
| 28 |
(file (compiler-message-file c)) |
| 29 |
(form (innermost-non-expanded-form (compiler-message-toplevel-form c)))) |
| 30 |
(if (and form |
| 31 |
position |
| 32 |
(not (minusp position)) |
| 33 |
(not (equalp form '|compiler preprocess|))) |
| 34 |
(let* ((*print-length* 2) |
| 35 |
(*print-level* 2)) |
| 36 |
(format stream |
| 37 |
"~A:~% in file ~A, position ~D~& at ~A" |
| 38 |
prefix |
| 39 |
(make-pathname :name (pathname-name file) |
| 40 |
:type (pathname-type file) |
| 41 |
:version (pathname-version file)) |
| 42 |
position |
| 43 |
form)) |
| 44 |
(format stream "~A:" prefix)) |
| 45 |
(format stream (compiler-message-format c) |
| 46 |
format-control |
| 47 |
format-arguments))) |
| 48 |
|
| 49 |
(define-condition compiler-message (simple-condition) |
| 50 |
((prefix :initform "Note" :accessor compiler-message-prefix) |
| 51 |
(format :initform +note-format+ :accessor compiler-message-format) |
| 52 |
(file :initarg :file :initform *compile-file-pathname* |
| 53 |
:accessor compiler-message-file) |
| 54 |
(position :initarg :file :initform *compile-file-position* |
| 55 |
:accessor compiler-message-file-position) |
| 56 |
(toplevel-form :initarg :form :initform *current-toplevel-form* |
| 57 |
:accessor compiler-message-toplevel-form) |
| 58 |
(form :initarg :form :initform *current-form* |
| 59 |
:accessor compiler-message-form)) |
| 60 |
(:report (lambda (c stream) |
| 61 |
(apply #'compiler-message-report stream c |
| 62 |
(simple-condition-format-control c) |
| 63 |
(simple-condition-format-arguments c))))) |
| 64 |
|
| 65 |
(define-condition compiler-note (compiler-message) ()) |
| 66 |
|
| 67 |
(define-condition compiler-debug-note (compiler-note) ()) |
| 68 |
|
| 69 |
(define-condition compiler-warning (compiler-message style-warning) |
| 70 |
((prefix :initform "Warning") |
| 71 |
(format :initform +warn-format+))) |
| 72 |
|
| 73 |
(define-condition compiler-macro-expansion-failed (compiler-warning) |
| 74 |
()) |
| 75 |
|
| 76 |
(define-condition compiler-error (compiler-message) |
| 77 |
((prefix :initform "Error") |
| 78 |
(format :initform +error-format+))) |
| 79 |
|
| 80 |
(define-condition compiler-fatal-error (compiler-error) |
| 81 |
((format :initform +fatal-format+))) |
| 82 |
|
| 83 |
(define-condition compiler-internal-error (compiler-fatal-error) |
| 84 |
((prefix :initform "Internal error"))) |
| 85 |
|
| 86 |
(define-condition compiler-style-warning (compiler-message style-warning) |
| 87 |
((prefix :initform "Style warning") |
| 88 |
(format :initform +warn-format+))) |
| 89 |
|
| 90 |
(define-condition compiler-undefined-variable (compiler-style-warning) |
| 91 |
((variable :initarg :name :initform nil)) |
| 92 |
(:report |
| 93 |
(lambda (c stream) |
| 94 |
(compiler-message-report stream c |
| 95 |
"Variable ~A was undefined. ~ |
| 96 |
Compiler assumes it is a global." |
| 97 |
(slot-value c 'variable))))) |
| 98 |
|
| 99 |
(define-condition circular-dependency (compiler-error) |
| 100 |
() |
| 101 |
(:report |
| 102 |
(lambda (c stream) |
| 103 |
(compiler-message-report stream c |
| 104 |
"Circular references in creation form for ~S." |
| 105 |
(compiler-message-form c))))) |
| 106 |
|
| 107 |
(define-condition dead-code (compiler-note style-warning) |
| 108 |
((dead-forms :initarg :dead-forms :initform nil) |
| 109 |
(context :initarg :context :initform nil) |
| 110 |
(explanation :initarg :explanation :initform "") |
| 111 |
(explanation-args :initarg :explanation-args :initform nil)) |
| 112 |
(:report |
| 113 |
(lambda (c stream) |
| 114 |
(let ((dead-forms (slot-value c 'dead-forms))) |
| 115 |
(compiler-message-report stream c |
| 116 |
"Eliminating the form~P ~{~S~^, ~} in ~S: ~?." |
| 117 |
(length dead-forms) |
| 118 |
(mapcar #'c1form-form dead-forms) |
| 119 |
(c1form-form (slot-value c 'context)) |
| 120 |
(slot-value c 'explanation) |
| 121 |
(mapcar #'c1form-form (slot-value c 'explanation-args))))))) |
| 122 |
|
| 123 |
(defun print-compiler-message (c stream) |
| 124 |
(unless (typep c *suppress-compiler-messages*) |
| 125 |
#+cmu-format |
| 126 |
(format stream "~&~@<;;; ~@;~A~:>" c) |
| 127 |
#-cmu-format |
| 128 |
(format stream "~&;;; ~A" c))) |
| 129 |
|
| 130 |
;;; A few notes about the following handlers. We want the user to be |
| 131 |
;;; able to capture, collect and perhaps abort on the different |
| 132 |
;;; conditions signaled by the compiler. Since the compiler uses |
| 133 |
;;; HANDLER-BIND, the only way to let this happen is either let the |
| 134 |
;;; handler return or use SIGNAL at the beginning of the handler and |
| 135 |
;;; let the outer handler intercept. |
| 136 |
;;; |
| 137 |
;;; In neither case do we want to enter the the debugger. That means |
| 138 |
;;; we can not derive the compiler conditions from SERIOUS-CONDITION. |
| 139 |
;;; |
| 140 |
(defun handle-compiler-note (c) |
| 141 |
(declare (ignore c)) |
| 142 |
nil) |
| 143 |
|
| 144 |
(defun handle-compiler-warning (c) |
| 145 |
(push c *compiler-conditions*) |
| 146 |
nil) |
| 147 |
|
| 148 |
(defun handle-compiler-error (c) |
| 149 |
(when *compiler-break-enable* |
| 150 |
(invoke-debugger c)) |
| 151 |
(signal c) |
| 152 |
(push c *compiler-conditions*) |
| 153 |
(print-compiler-message c t) |
| 154 |
(abort)) |
| 155 |
|
| 156 |
(defun handle-compiler-internal-error (c) |
| 157 |
(when *compiler-break-enable* |
| 158 |
(invoke-debugger c)) |
| 159 |
(setf c (make-condition 'compiler-internal-error |
| 160 |
:format-control "~A" |
| 161 |
:format-arguments (list c))) |
| 162 |
(push c *compiler-conditions*) |
| 163 |
(signal c) |
| 164 |
(print-compiler-message c t) |
| 165 |
(abort)) |
| 166 |
|
| 167 |
(defmacro cmpck (condition string &rest args) |
| 168 |
`(if ,condition (cmperr ,string ,@args))) |
| 169 |
|
| 170 |
(defmacro cmpassert (condition string &rest args) |
| 171 |
`(unless ,condition (cmperr ,string ,@args))) |
| 172 |
|
| 173 |
(defun cmperr (string &rest args) |
| 174 |
(let ((c (make-condition 'compiler-error |
| 175 |
:format-control string |
| 176 |
:format-arguments args))) |
| 177 |
(signal c) |
| 178 |
(print-compiler-message c t) |
| 179 |
(abort))) |
| 180 |
|
| 181 |
(defun too-many-args (name upper-bound n &aux (*print-case* :upcase)) |
| 182 |
(cmperr "~S requires at most ~R argument~:p, but ~R ~:*~[were~;was~:;were~] supplied.~%" |
| 183 |
name upper-bound n)) |
| 184 |
|
| 185 |
(defun too-few-args (name lower-bound n) |
| 186 |
(cmperr "~S requires at least ~R argument~:p, but only ~R ~:*~[were~;was~:;were~] supplied.~%" |
| 187 |
name lower-bound n)) |
| 188 |
|
| 189 |
(defun do-cmpwarn (&rest args) |
| 190 |
(declare (si::c-local)) |
| 191 |
(let ((condition (apply #'make-condition args))) |
| 192 |
(restart-case (signal condition) |
| 193 |
(muffle-warning () |
| 194 |
:REPORT "Skip warning" |
| 195 |
(return-from do-cmpwarn nil))) |
| 196 |
(print-compiler-message condition t))) |
| 197 |
|
| 198 |
(defun cmpwarn-style (string &rest args) |
| 199 |
(do-cmpwarn 'compiler-style-warning :format-control string :format-arguments args)) |
| 200 |
|
| 201 |
(defun cmpwarn (string &rest args) |
| 202 |
(do-cmpwarn 'compiler-warning :format-control string :format-arguments args)) |
| 203 |
|
| 204 |
(defun cmpnote (string &rest args) |
| 205 |
(do-cmpwarn 'compiler-note :format-control string :format-arguments args)) |
| 206 |
|
| 207 |
(defun cmpdebug (string &rest args) |
| 208 |
(do-cmpwarn 'compiler-debug-note :format-control string :format-arguments args)) |
| 209 |
|
| 210 |
(defun undefined-variable (sym) |
| 211 |
(do-cmpwarn 'compiler-undefined-variable :name sym)) |
| 212 |
|
| 213 |
(defun warn-dead-code (dead-forms context explanation &rest explanation-args) |
| 214 |
(do-cmpwarn 'dead-code |
| 215 |
:dead-forms (if (listp dead-forms) |
| 216 |
dead-forms |
| 217 |
(list dead-forms)) |
| 218 |
:context context |
| 219 |
:explanation explanation |
| 220 |
:explanation-args explanation-args)) |
| 221 |
|
| 222 |
(defun baboon (&key (format-control "A bug was found in the compiler") |
| 223 |
format-arguments) |
| 224 |
(signal 'compiler-internal-error |
| 225 |
:format-control format-control |
| 226 |
:format-arguments format-arguments)) |
| 227 |
|
| 228 |
;;; This is not used (left for debugging). |
| 229 |
(defmacro with-cmp-protection (main-form error-form) |
| 230 |
`(let* ((si::*break-enable* *compiler-break-enable*) |
| 231 |
(throw-flag t)) |
| 232 |
(unwind-protect |
| 233 |
(multiple-value-prog1 |
| 234 |
(if *compiler-break-enable* |
| 235 |
(handler-bind ((error #'invoke-debugger)) |
| 236 |
,main-form) |
| 237 |
,main-form) |
| 238 |
(setf throw-flag nil)) |
| 239 |
(when throw-flag ,error-form)))) |