glib-2.0: Add g_unichar_ismark binding
[vala-lang.git] / vala / valareport.vala
blob8d3ece97c457c747df35dd67412770659ad90841
1 /* valareport.vala
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
19 * Author:
20 * Jürg Billeter <j@bitron.ch>
23 using GLib;
25 /**
26 * Namespace to centralize reporting warnings and errors.
28 public class Vala.Report : Object {
29 int warnings;
30 int errors;
32 bool verbose_errors;
34 public bool enable_warnings { get; set; default = true; }
36 /**
37 * Set the error verbosity.
39 public void set_verbose_errors (bool verbose) {
40 verbose_errors = verbose;
43 /**
44 * Returns the total number of warnings reported.
46 public int get_warnings () {
47 return warnings;
50 /**
51 * Returns the total number of errors reported.
53 public int get_errors () {
54 return errors;
57 /**
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
63 return;
66 string offending_line = source.file.get_source_line (source.first_line);
68 if (offending_line != null) {
69 stderr.printf ("%s\n", offending_line);
70 int idx;
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') {
77 stderr.printf ("\t");
78 } else {
79 stderr.printf (" ");
83 for (idx = source.first_column; idx <= source.last_column; ++idx) {
84 if (offending_line[idx - 1] == '\t') {
85 stderr.printf ("\t");
86 } else {
87 stderr.printf ("^");
91 stderr.printf ("\n");
95 /**
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) {
103 return;
106 if (source == null) {
107 stderr.printf ("note: %s\n", message);
108 } else {
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) {
124 return;
127 warnings++;
128 if (source == null) {
129 stderr.printf ("warning: %s\n", message);
130 } else {
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) {
143 return;
146 warnings++;
147 if (source == null) {
148 stderr.printf ("warning: %s\n", message);
149 } else {
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) {
164 errors++;
165 if (source == null) {
166 stderr.printf ("error: %s\n", message);
167 } else {
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);