Do not free values returned via g_object_get prematurely, require
[vala-lang.git] / vala / valareport.vala
blobc77c8975dfe8a3ef4d6591217563f4360eb53720
1 /* valareport.vala
3 * Copyright (C) 2006-2008 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 namespace Vala.Report {
29 public int warnings;
30 public int errors;
32 public bool verbose_errors;
34 /**
35 * Set the error verbosity.
37 public void set_verbose_errors (bool verbose) {
38 verbose_errors = verbose;
41 /**
42 * Returns the total number of warnings reported.
44 public int get_warnings () {
45 return warnings;
48 /**
49 * Returns the total number of errors reported.
51 public int get_errors () {
52 return errors;
55 /**
56 * Pretty-print the actual line of offending code if possible.
58 public void report_source (SourceReference source) {
59 if (source.first_line != source.last_line) {
60 // FIXME Cannot report multi-line issues currently
61 return;
64 string offending_line = source.file.get_source_line (source.first_line);
66 if (offending_line != null) {
67 stderr.printf ("%s\n", offending_line);
68 int idx;
70 /* We loop in this manner so that we don't fall over on differing
71 * tab widths. This means we get the ^s in the right places.
73 for (idx = 1; idx < source.first_column; ++idx) {
74 if (offending_line[idx - 1] == '\t') {
75 stderr.printf ("\t");
76 } else {
77 stderr.printf (" ");
81 for (idx = source.first_column; idx <= source.last_column; ++idx) {
82 if (offending_line[idx - 1] == '\t') {
83 stderr.printf ("\t");
84 } else {
85 stderr.printf ("^");
89 stderr.printf ("\n");
93 /**
94 * Reports the specified message as warning.
96 * @param source reference to source code
97 * @param message warning message
99 public void warning (SourceReference? source, string message) {
100 warnings++;
101 if (source == null) {
102 stderr.printf ("warning: %s\n", message);
103 } else {
104 stderr.printf ("%s: warning: %s\n", source.to_string (), message);
105 if (verbose_errors) {
106 report_source (source);
112 * Reports the specified message as error.
114 * @param source reference to source code
115 * @param message error message
117 public void error (SourceReference? source, string message) {
118 errors++;
119 if (source == null) {
120 stderr.printf ("error: %s\n", message);
121 } else {
122 stderr.printf ("%s: error: %s\n", source.to_string (), message);
123 if (verbose_errors) {
124 report_source (source);