message.scm 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  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. (define (format-loc file line column)
  36. (format #f "~a:~a:~a"
  37. (or file "<stdin>")
  38. (1+ line)
  39. column))
  40. (match loc
  41. (#(file line column)
  42. (format-loc file line column))
  43. ((? pair? loc)
  44. (format-loc (assoc-ref loc 'filename)
  45. (assoc-ref loc 'line)
  46. (assoc-ref loc 'column)))
  47. (_ "<unknown-location>")))
  48. ;;;
  49. ;;; Warnings
  50. ;;;
  51. ;; This name existed before %current-warning-port was introduced, but
  52. ;; otherwise it is a deprecated binding.
  53. (define *current-warning-port*
  54. ;; Can't play the identifier-syntax deprecation game in Guile 2.0, as
  55. ;; other modules might depend on this being a normal binding and not a
  56. ;; syntax binding.
  57. (parameter-fluid current-warning-port))
  58. (define *current-warning-prefix*
  59. ;; Prefix string when emitting a warning.
  60. (make-fluid ";;; "))
  61. (define-record-type <warning-type>
  62. (make-warning-type name description printer)
  63. warning-type?
  64. (name warning-type-name)
  65. (description warning-type-description)
  66. (printer warning-type-printer))
  67. (define %warning-types
  68. ;; List of known warning types.
  69. (map (lambda (args)
  70. (apply make-warning-type args))
  71. (let-syntax ((emit
  72. (lambda (s)
  73. (syntax-case s ()
  74. ((_ port fmt args ...)
  75. (string? (syntax->datum #'fmt))
  76. (with-syntax ((fmt
  77. (string-append "~a"
  78. (syntax->datum
  79. #'fmt))))
  80. #'(format port fmt
  81. (fluid-ref *current-warning-prefix*)
  82. args ...)))))))
  83. `((unsupported-warning ;; a "meta warning"
  84. "warn about unknown warning types"
  85. ,(lambda (port unused name)
  86. (emit port "warning: unknown warning type `~A'~%"
  87. name)))
  88. (unused-variable
  89. "report unused variables"
  90. ,(lambda (port loc name)
  91. (emit port "~A: warning: unused variable `~A'~%"
  92. loc name)))
  93. (unused-toplevel
  94. "report unused local top-level variables"
  95. ,(lambda (port loc name)
  96. (emit port "~A: warning: possibly unused local top-level variable `~A'~%"
  97. loc name)))
  98. (shadowed-toplevel
  99. "report shadowed top-level variables"
  100. ,(lambda (port loc name previous-loc)
  101. (emit port "~A: warning: shadows previous definition of `~A' at ~A~%"
  102. loc name
  103. (location-string previous-loc))))
  104. (unbound-variable
  105. "report possibly unbound variables"
  106. ,(lambda (port loc name)
  107. (emit port "~A: warning: possibly unbound variable `~A'~%"
  108. loc name)))
  109. (macro-use-before-definition
  110. "report possibly mis-use of macros before they are defined"
  111. ,(lambda (port loc name)
  112. (emit port "~A: warning: macro `~A' used before definition~%"
  113. loc name)))
  114. (use-before-definition
  115. "report uses of top-levels before they are defined"
  116. ,(lambda (port loc name)
  117. (emit port "~A: warning: `~A' used before definition~%"
  118. loc name)))
  119. (non-idempotent-definition
  120. "report names that can refer to imports on first load, but module definitions on second load"
  121. ,(lambda (port loc name)
  122. (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.~%"
  123. loc name name)))
  124. (arity-mismatch
  125. "report procedure arity mismatches (wrong number of arguments)"
  126. ,(lambda (port loc name certain?)
  127. (if certain?
  128. (emit port
  129. "~A: warning: wrong number of arguments to `~A'~%"
  130. loc name)
  131. (emit port
  132. "~A: warning: possibly wrong number of arguments to `~A'~%"
  133. loc name))))
  134. (duplicate-case-datum
  135. "report a duplicate datum in a case expression"
  136. ,(lambda (port loc datum clause case-expr)
  137. (emit port
  138. "~A: warning: duplicate datum ~S in clause ~S of case expression ~S~%"
  139. loc datum clause case-expr)))
  140. (bad-case-datum
  141. "report a case datum that cannot be meaningfully compared using `eqv?'"
  142. ,(lambda (port loc datum clause case-expr)
  143. (emit port
  144. "~A: warning: datum ~S cannot be meaningfully compared using `eqv?' in clause ~S of case expression ~S~%"
  145. loc datum clause case-expr)))
  146. (format
  147. "report wrong number of arguments to `format'"
  148. ,(lambda (port loc . rest)
  149. (define (escape-newlines str)
  150. (list->string
  151. (string-fold-right (lambda (c r)
  152. (if (eq? c #\newline)
  153. (append '(#\\ #\n) r)
  154. (cons c r)))
  155. '()
  156. str)))
  157. (define (range min max)
  158. (cond ((eq? min 'any)
  159. (if (eq? max 'any)
  160. "any number" ;; can't happen
  161. (emit #f "up to ~a" max)))
  162. ((eq? max 'any)
  163. (emit #f "at least ~a" min))
  164. ((= min max) (number->string min))
  165. (else
  166. (emit #f "~a to ~a" min max))))
  167. (match rest
  168. (('simple-format fmt opt)
  169. (emit port
  170. "~A: warning: ~S: unsupported format option ~~~A, use (ice-9 format) instead~%"
  171. loc (escape-newlines fmt) opt))
  172. (('wrong-format-arg-count fmt min max actual)
  173. (emit port
  174. "~A: warning: ~S: wrong number of `format' arguments: expected ~A, got ~A~%"
  175. loc (escape-newlines fmt)
  176. (range min max) actual))
  177. (('syntax-error 'unterminated-iteration fmt)
  178. (emit port "~A: warning: ~S: unterminated iteration~%"
  179. loc (escape-newlines fmt)))
  180. (('syntax-error 'unterminated-conditional fmt)
  181. (emit port "~A: warning: ~S: unterminated conditional~%"
  182. loc (escape-newlines fmt)))
  183. (('syntax-error 'unexpected-semicolon fmt)
  184. (emit port "~A: warning: ~S: unexpected `~~;'~%"
  185. loc (escape-newlines fmt)))
  186. (('syntax-error 'unexpected-conditional-termination fmt)
  187. (emit port "~A: warning: ~S: unexpected `~~]'~%"
  188. loc (escape-newlines fmt)))
  189. (('wrong-port wrong-port)
  190. (emit port
  191. "~A: warning: ~S: wrong port argument~%"
  192. loc wrong-port))
  193. (('wrong-format-string fmt)
  194. (emit port
  195. "~A: warning: ~S: wrong format string~%"
  196. loc fmt))
  197. (('non-literal-format-string)
  198. (emit port
  199. "~A: warning: non-literal format string~%"
  200. loc))
  201. (('wrong-num-args count)
  202. (emit port
  203. "~A: warning: wrong number of arguments to `format'~%"
  204. loc))
  205. (else
  206. (emit port "~A: `format' warning~%" loc)))))))))
  207. (define (lookup-warning-type name)
  208. "Return the warning type NAME or `#f' if not found."
  209. (find (lambda (wt)
  210. (eq? name (warning-type-name wt)))
  211. %warning-types))
  212. (define (warning type location . args)
  213. "Emit a warning of type TYPE for source location LOCATION (a source
  214. property alist) using the data in ARGS."
  215. (let ((wt (lookup-warning-type type))
  216. (port (current-warning-port)))
  217. (if (warning-type? wt)
  218. (apply (warning-type-printer wt)
  219. port (location-string location)
  220. args)
  221. (format port "~A: unknown warning type `~A': ~A~%"
  222. (location-string location) type args))))