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 warning.
119 * @param source reference to source code
120 * @param message warning message
122 public virtual void warn (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
);
132 if (verbose_errors
) {
133 report_source (source
);
139 * Reports the specified message as error.
141 * @param source reference to source code
142 * @param message error message
144 public virtual void err (SourceReference? source
, string message
) {
146 if (source
== null) {
147 stderr
.printf ("error: %s\n", message
);
149 stderr
.printf ("%s: error: %s\n", source
.to_string (), message
);
150 if (verbose_errors
) {
151 report_source (source
);
156 /* Convenience methods calling warn and err on correct instance */
157 public static void notice (SourceReference? source
, string message
) {
158 CodeContext
.get ().report
.note (source
, message
);
160 public static void warning (SourceReference? source
, string message
) {
161 CodeContext
.get ().report
.warn (source
, message
);
163 public static void error (SourceReference? source
, string message
) {
164 CodeContext
.get ().report
.err (source
, message
);