message.scm 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  1. ;;; User interface messages
  2. ;; Copyright (C) 2009-2012,2016,2018,2020-2021 Free Software Foundation, Inc.
  3. ;;; This library is free software; you can redistribute it and/or modify it
  4. ;;; under the terms of the GNU Lesser General Public License as published by
  5. ;;; the Free Software Foundation; either version 3 of the License, or (at
  6. ;;; your option) any later version.
  7. ;;;
  8. ;;; This library is distributed in the hope that it will be useful, but
  9. ;;; WITHOUT ANY WARRANTY; without even the implied warranty of
  10. ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser
  11. ;;; General Public License for more details.
  12. ;;;
  13. ;;; You should have received a copy of the GNU Lesser General Public License
  14. ;;; along with this program. If not, see <http://www.gnu.org/licenses/>.
  15. ;;; Commentary:
  16. ;;;
  17. ;;; This module provide a simple interface to send messages to the user.
  18. ;;; TODO: Internationalize messages.
  19. ;;;
  20. ;;; Code:
  21. (define-module (system base message)
  22. #:use-module (srfi srfi-1)
  23. #:use-module (srfi srfi-9)
  24. #:use-module (ice-9 match)
  25. #:export (*current-warning-port*
  26. *current-warning-prefix*
  27. warning
  28. warning-type? warning-type-name warning-type-description
  29. warning-type-printer lookup-warning-type
  30. %warning-types))
  31. ;;;
  32. ;;; Source location
  33. ;;;
  34. (define (location-string loc)
  35. (if (pair? loc)
  36. (format #f "~a:~a:~a"
  37. (or (assoc-ref loc 'filename) "<stdin>")
  38. (1+ (assoc-ref loc 'line))
  39. (assoc-ref loc 'column))
  40. "<unknown-location>"))
  41. ;;;
  42. ;;; Warnings
  43. ;;;
  44. ;; This name existed before %current-warning-port was introduced, but
  45. ;; otherwise it is a deprecated binding.
  46. (define *current-warning-port*
  47. ;; Can't play the identifier-syntax deprecation game in Guile 2.0, as
  48. ;; other modules might depend on this being a normal binding and not a
  49. ;; syntax binding.
  50. (parameter-fluid current-warning-port))
  51. (define *current-warning-prefix*
  52. ;; Prefix string when emitting a warning.
  53. (make-fluid ";;; "))
  54. (define-record-type <warning-type>
  55. (make-warning-type name description printer)
  56. warning-type?
  57. (name warning-type-name)
  58. (description warning-type-description)
  59. (printer warning-type-printer))
  60. (define %warning-types
  61. ;; List of known warning types.
  62. (map (lambda (args)
  63. (apply make-warning-type args))
  64. (let-syntax ((emit
  65. (lambda (s)
  66. (syntax-case s ()
  67. ((_ port fmt args ...)
  68. (string? (syntax->datum #'fmt))
  69. (with-syntax ((fmt
  70. (string-append "~a"
  71. (syntax->datum
  72. #'fmt))))
  73. #'(format port fmt
  74. (fluid-ref *current-warning-prefix*)
  75. args ...)))))))
  76. `((unsupported-warning ;; a "meta warning"
  77. "warn about unknown warning types"
  78. ,(lambda (port unused name)
  79. (emit port "warning: unknown warning type `~A'~%"
  80. name)))
  81. (unused-variable
  82. "report unused variables"
  83. ,(lambda (port loc name)
  84. (emit port "~A: warning: unused variable `~A'~%"
  85. loc name)))
  86. (unused-toplevel
  87. "report unused local top-level variables"
  88. ,(lambda (port loc name)
  89. (emit port "~A: warning: possibly unused local top-level variable `~A'~%"
  90. loc name)))
  91. (shadowed-toplevel
  92. "report shadowed top-level variables"
  93. ,(lambda (port loc name previous-loc)
  94. (emit port "~A: warning: shadows previous definition of `~A' at ~A~%"
  95. loc name
  96. (location-string previous-loc))))
  97. (unbound-variable
  98. "report possibly unbound variables"
  99. ,(lambda (port loc name)
  100. (emit port "~A: warning: possibly unbound variable `~A'~%"
  101. loc name)))
  102. (macro-use-before-definition
  103. "report possibly mis-use of macros before they are defined"
  104. ,(lambda (port loc name)
  105. (emit port "~A: warning: macro `~A' used before definition~%"
  106. loc name)))
  107. (use-before-definition
  108. "report uses of top-levels before they are defined"
  109. ,(lambda (port loc name)
  110. (emit port "~A: warning: `~A' used before definition~%"
  111. loc name)))
  112. (non-idempotent-definition
  113. "report names that can refer to imports on first load, but module definitions on second load"
  114. ,(lambda (port loc name)
  115. (emit port "~A: warning: non-idempotent binding for `~A'. When first loaded, value for `~A` comes from imported binding, but later module-local definition overrides it; any module reload would capture module-local binding rather than import.~%"
  116. loc name name)))
  117. (arity-mismatch
  118. "report procedure arity mismatches (wrong number of arguments)"
  119. ,(lambda (port loc name certain?)
  120. (if certain?
  121. (emit port
  122. "~A: warning: wrong number of arguments to `~A'~%"
  123. loc name)
  124. (emit port
  125. "~A: warning: possibly wrong number of arguments to `~A'~%"
  126. loc name))))
  127. (duplicate-case-datum
  128. "report a duplicate datum in a case expression"
  129. ,(lambda (port loc datum clause case-expr)
  130. (emit port
  131. "~A: warning: duplicate datum ~S in clause ~S of case expression ~S~%"
  132. loc datum clause case-expr)))
  133. (bad-case-datum
  134. "report a case datum that cannot be meaningfully compared using `eqv?'"
  135. ,(lambda (port loc datum clause case-expr)
  136. (emit port
  137. "~A: warning: datum ~S cannot be meaningfully compared using `eqv?' in clause ~S of case expression ~S~%"
  138. loc datum clause case-expr)))
  139. (format
  140. "report wrong number of arguments to `format'"
  141. ,(lambda (port loc . rest)
  142. (define (escape-newlines str)
  143. (list->string
  144. (string-fold-right (lambda (c r)
  145. (if (eq? c #\newline)
  146. (append '(#\\ #\n) r)
  147. (cons c r)))
  148. '()
  149. str)))
  150. (define (range min max)
  151. (cond ((eq? min 'any)
  152. (if (eq? max 'any)
  153. "any number" ;; can't happen
  154. (emit #f "up to ~a" max)))
  155. ((eq? max 'any)
  156. (emit #f "at least ~a" min))
  157. ((= min max) (number->string min))
  158. (else
  159. (emit #f "~a to ~a" min max))))
  160. (match rest
  161. (('simple-format fmt opt)
  162. (emit port
  163. "~A: warning: ~S: unsupported format option ~~~A, use (ice-9 format) instead~%"
  164. loc (escape-newlines fmt) opt))
  165. (('wrong-format-arg-count fmt min max actual)
  166. (emit port
  167. "~A: warning: ~S: wrong number of `format' arguments: expected ~A, got ~A~%"
  168. loc (escape-newlines fmt)
  169. (range min max) actual))
  170. (('syntax-error 'unterminated-iteration fmt)
  171. (emit port "~A: warning: ~S: unterminated iteration~%"
  172. loc (escape-newlines fmt)))
  173. (('syntax-error 'unterminated-conditional fmt)
  174. (emit port "~A: warning: ~S: unterminated conditional~%"
  175. loc (escape-newlines fmt)))
  176. (('syntax-error 'unexpected-semicolon fmt)
  177. (emit port "~A: warning: ~S: unexpected `~~;'~%"
  178. loc (escape-newlines fmt)))
  179. (('syntax-error 'unexpected-conditional-termination fmt)
  180. (emit port "~A: warning: ~S: unexpected `~~]'~%"
  181. loc (escape-newlines fmt)))
  182. (('wrong-port wrong-port)
  183. (emit port
  184. "~A: warning: ~S: wrong port argument~%"
  185. loc wrong-port))
  186. (('wrong-format-string fmt)
  187. (emit port
  188. "~A: warning: ~S: wrong format string~%"
  189. loc fmt))
  190. (('non-literal-format-string)
  191. (emit port
  192. "~A: warning: non-literal format string~%"
  193. loc))
  194. (('wrong-num-args count)
  195. (emit port
  196. "~A: warning: wrong number of arguments to `format'~%"
  197. loc))
  198. (else
  199. (emit port "~A: `format' warning~%" loc)))))))))
  200. (define (lookup-warning-type name)
  201. "Return the warning type NAME or `#f' if not found."
  202. (find (lambda (wt)
  203. (eq? name (warning-type-name wt)))
  204. %warning-types))
  205. (define (warning type location . args)
  206. "Emit a warning of type TYPE for source location LOCATION (a source
  207. property alist) using the data in ARGS."
  208. (let ((wt (lookup-warning-type type))
  209. (port (current-warning-port)))
  210. (if (warning-type? wt)
  211. (apply (warning-type-printer wt)
  212. port (location-string location)
  213. args)
  214. (format port "~A: unknown warning type `~A': ~A~%"
  215. (location-string location) type args))))