1 ;;; GNU Guix --- Functional package management for GNU
2 ;;; Copyright © 2012, 2013, 2014, 2015, 2016, 2017, 2018, 2019 Ludovic Courtès <ludo@gnu.org>
4 ;;; This file is part of GNU Guix.
6 ;;; GNU Guix is free software; you can redistribute it and/or modify it
7 ;;; under the terms of the GNU General Public License as published by
8 ;;; the Free Software Foundation; either version 3 of the License, or (at
9 ;;; your option) any later version.
11 ;;; GNU Guix is distributed in the hope that it will be useful, but
12 ;;; WITHOUT ANY WARRANTY; without even the implied warranty of
13 ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 ;;; GNU General Public License for more details.
16 ;;; You should have received a copy of the GNU General Public License
17 ;;; along with GNU Guix. If not, see <http://www.gnu.org/licenses/>.
19 (define-module (guix diagnostics)
20 #:use-module (guix colors)
21 #:use-module (guix i18n)
22 #:autoload (guix utils) (<location>)
23 #:use-module (srfi srfi-26)
24 #:use-module (ice-9 format)
25 #:use-module (ice-9 match)
38 ;;; This module provides the tools to report diagnostics to the user in a
39 ;;; consistent way: errors, warnings, and notes.
43 (define-syntax highlight-argument
45 "Given FMT and ARG, expand ARG to a call that highlights it, provided FMT
46 is a trivial format string."
47 (define (trivial-format-string? fmt)
52 (or (>= (+ 1 start) len)
53 (let ((tilde (string-index fmt #\~ start)))
55 (case (string-ref fmt (+ tilde 1))
56 ((#\a #\A #\%) (loop (+ tilde 2)))
59 ;; Be conservative: limit format argument highlighting to cases where the
60 ;; format string contains nothing but ~a escapes. If it contained ~s
61 ;; escapes, this strategy wouldn't work.
63 ((_ "~a~%" arg) ;don't highlight whole messages
66 (trivial-format-string? (syntax->datum #'fmt))
67 #'(%highlight-argument arg))
71 (define* (%highlight-argument arg #:optional (port (guix-warning-port)))
72 "Highlight ARG, a format string argument, if PORT supports colors."
76 (highlight (symbol->string arg) port))
79 (define-syntax define-diagnostic
81 "Create a diagnostic macro (i.e., NAME), which will prepend PREFIX to all
83 ((_ name (G_ prefix) colors)
87 ((name location (underscore fmt) args (... ...))
88 (and (string? (syntax->datum #'fmt))
89 (free-identifier=? #'underscore #'G_))
91 (print-diagnostic-prefix prefix location
93 (format (guix-warning-port) (gettext fmt %gettext-domain)
94 (highlight-argument fmt args) (... ...))))
95 ((name location (N-underscore singular plural n)
97 (and (string? (syntax->datum #'singular))
98 (string? (syntax->datum #'plural))
99 (free-identifier=? #'N-underscore #'N_))
101 (print-diagnostic-prefix prefix location
103 (format (guix-warning-port)
104 (ngettext singular plural n %gettext-domain)
105 (highlight-argument singular args) (... ...))))
106 ((name (underscore fmt) args (... ...))
107 (free-identifier=? #'underscore #'G_)
108 #'(name #f (underscore fmt) args (... ...)))
109 ((name (N-underscore singular plural n)
111 (free-identifier=? #'N-underscore #'N_)
112 #'(name #f (N-underscore singular plural n)
113 args (... ...)))))))))
115 ;; XXX: This doesn't work well for right-to-left languages.
116 ;; TRANSLATORS: The goal is to emit "warning:" followed by a short phrase;
117 ;; "~a" is a placeholder for that phrase.
118 (define-diagnostic warning (G_ "warning: ") %warning-color) ;emit a warning
119 (define-diagnostic info (G_ "") %info-color)
120 (define-diagnostic report-error (G_ "error: ") %error-color)
122 (define-syntax-rule (leave args ...)
123 "Emit an error message and exit."
125 (report-error args ...)
128 (define %warning-color (color BOLD MAGENTA))
129 (define %info-color (color BOLD))
130 (define %error-color (color BOLD RED))
132 (define* (print-diagnostic-prefix prefix #:optional location
133 #:key (colors (color)))
134 "Print PREFIX as a diagnostic line prefix."
136 (color-output? (guix-warning-port)))
138 (define location-color
140 (cut colorize-string <> (color BOLD))
146 (colorize-string prefix colors))
149 (let ((prefix (if (string-null? prefix)
151 (gettext prefix %gettext-domain))))
153 (format (guix-warning-port) "~a: ~a"
154 (location-color (location->string location))
155 (prefix-color prefix))
156 (format (guix-warning-port) "~:[~*~;guix ~a: ~]~a"
157 (program-name) (program-name)
158 (prefix-color prefix)))))
160 (define (location->string loc)
161 "Return a human-friendly, GNU-standard representation of LOC."
163 (#f (G_ "<unknown location>"))
164 (($ <location> file line column)
165 (format #f "~a:~a:~a" file line column))))
168 (define guix-warning-port
169 (make-parameter (current-warning-port)))
172 ;; Name of the command-line program currently executing, or #f.