3 * Copyright (C) 2006-2010 Jürg Billeter
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2.1 of the License, or (at your option) any later version.
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20 * Jürg Billeter <j@bitron.ch>
26 * Namespace to centralize reporting warnings and errors.
28 public class Vala
.Report
: Object
{
34 public bool enable_warnings
{ get; set; default = true; }
37 * Set the error verbosity.
39 public void set_verbose_errors (bool verbose
) {
40 verbose_errors
= verbose
;
44 * Returns the total number of warnings reported.
46 public int get_warnings () {
51 * Returns the total number of errors reported.
53 public int get_errors () {
58 * Pretty-print the actual line of offending code if possible.
60 static void report_source (SourceReference source
) {
61 if (source
.first_line
!= source
.last_line
) {
62 // FIXME Cannot report multi-line issues currently
66 string offending_line
= source
.file
.get_source_line (source
.first_line
);
68 if (offending_line
!= null) {
69 stderr
.printf ("%s\n", offending_line
);
72 /* We loop in this manner so that we don't fall over on differing
73 * tab widths. This means we get the ^s in the right places.
75 for (idx
= 1; idx
< source
.first_column
; ++idx
) {
76 if (offending_line
[idx
- 1] == '\t') {
83 for (idx
= source
.first_column
; idx
<= source
.last_column
; ++idx
) {
84 if (offending_line
[idx
- 1] == '\t') {
96 * Reports the specified message as note.
98 * @param source reference to source code
99 * @param message note message
101 public virtual void note (SourceReference? source
, string message
) {
102 if (!enable_warnings
) {
106 if (source
== null) {
107 stderr
.printf ("note: %s\n", message
);
109 stderr
.printf ("%s: note: %s\n", source
.to_string (), message
);
110 if (verbose_errors
) {
111 report_source (source
);
117 * Reports the specified message as deprecation warning.
119 * @param source reference to source code
120 * @param message warning message
122 public virtual void depr (SourceReference? source
, string message
) {
123 if (!enable_warnings
) {
128 if (source
== null) {
129 stderr
.printf ("warning: %s\n", message
);
131 stderr
.printf ("%s: warning: %s\n", source
.to_string (), message
);
136 * Reports the specified message as warning.
138 * @param source reference to source code
139 * @param message warning message
141 public virtual void warn (SourceReference? source
, string message
) {
142 if (!enable_warnings
) {
147 if (source
== null) {
148 stderr
.printf ("warning: %s\n", message
);
150 stderr
.printf ("%s: warning: %s\n", source
.to_string (), message
);
151 if (verbose_errors
) {
152 report_source (source
);
158 * Reports the specified message as error.
160 * @param source reference to source code
161 * @param message error message
163 public virtual void err (SourceReference? source
, string message
) {
165 if (source
== null) {
166 stderr
.printf ("error: %s\n", message
);
168 stderr
.printf ("%s: error: %s\n", source
.to_string (), message
);
169 if (verbose_errors
) {
170 report_source (source
);
175 /* Convenience methods calling warn and err on correct instance */
176 public static void notice (SourceReference? source
, string message
) {
177 CodeContext
.get ().report
.note (source
, message
);
179 public static void deprecated (SourceReference? source
, string message
) {
180 CodeContext
.get ().report
.depr (source
, message
);
182 public static void experimental (SourceReference? source
, string message
) {
183 CodeContext
.get ().report
.depr (source
, message
);
185 public static void warning (SourceReference? source
, string message
) {
186 CodeContext
.get ().report
.warn (source
, message
);
188 public static void error (SourceReference? source
, string message
) {
189 CodeContext
.get ().report
.err (source
, message
);