2 // report.cs: report errors and warnings.
4 // Author: Miguel de Icaza (miguel@ximian.com)
6 // (C) 2001 Ximian, Inc. (http://www.ximian.com)
10 // FIXME: currently our class library does not support custom number format strings
14 using System.Collections;
15 using System.Diagnostics;
17 namespace Mono.CSharp {
20 /// This class is used to report errors and warnings t te user.
24 /// Errors encountered so far
26 static public int Errors;
29 /// Warnings encountered so far
31 static public int Warnings;
34 /// Whether errors should be throw an exception
36 static public bool Fatal;
39 /// Whether warnings should be considered errors
41 static public bool WarningsAreErrors;
44 /// Whether to dump a stack trace on errors.
46 static public bool Stacktrace;
49 // If the 'expected' error code is reported then the
50 // compilation succeeds.
52 // Used for the test suite to excercise the error codes
54 static int expected_error = 0;
57 // Keeps track of the warnings that we are ignoring
59 static Hashtable warning_ignore_table;
61 static void Check (int code)
63 if (code == expected_error){
65 throw new Exception ();
71 static public void RealError (string msg)
74 Console.WriteLine (msg);
77 Console.WriteLine (new StackTrace ().ToString ());
79 throw new Exception (msg);
82 static public void Error (int code, Location l, string text)
84 string msg = String.Format (
85 "{0}({1}) error CS{2:0000}: {3}", l.Name, l.Row, code, text);
86 // "{0}({1}) error CS{2}: {3}", l.Name, l.Row, code, text);
92 static public void Warning (int code, Location l, string text)
94 if (warning_ignore_table != null){
95 if (warning_ignore_table.Contains (code))
99 if (WarningsAreErrors)
100 Error (code, l, text);
104 if (Location.IsNull (l))
107 row = l.Row.ToString ();
109 Console.WriteLine (String.Format (
110 "{0}({1}) warning CS{2:0000}: {3}",
111 // "{0}({1}) warning CS{2}: {3}",
112 l.Name, row, code, text));
117 Console.WriteLine (new StackTrace ().ToString ());
121 static public void Warning (int code, string text)
123 Warning (code, Location.Null, text);
126 static public void Error (int code, string text)
128 string msg = String.Format ("error CS{0:0000}: {1}", code, text);
129 // string msg = String.Format ("error CS{0}: {1}", code, text);
135 static public void Message (Message m)
137 if (m is ErrorMessage)
138 Error (m.code, m.text);
140 Warning (m.code, m.text);
143 static public void SetIgnoreWarning (int code)
145 if (warning_ignore_table == null)
146 warning_ignore_table = new Hashtable ();
148 warning_ignore_table [code] = true;
151 static public int ExpectedError {
153 expected_error = value;
156 return expected_error;
160 public static int DebugFlags = 0;
162 [Conditional ("MCS_DEBUG")]
163 static public void Debug (string message, params object[] args)
165 Debug (4, message, args);
168 [Conditional ("MCS_DEBUG")]
169 static public void Debug (int category, string message, params object[] args)
171 if ((category & DebugFlags) == 0)
174 StringBuilder sb = new StringBuilder (message);
176 if ((args != null) && (args.Length > 0)) {
180 foreach (object arg in args) {
187 else if (arg is ICollection)
188 sb.Append (PrintCollection ((ICollection) arg));
194 Console.WriteLine (sb.ToString ());
197 static public string PrintCollection (ICollection collection)
199 StringBuilder sb = new StringBuilder ();
201 sb.Append (collection.GetType ());
205 foreach (object o in collection) {
214 return sb.ToString ();
218 public class Message {
222 public Message (int code, string text)
229 public class WarningMessage : Message {
230 public WarningMessage (int code, string text) : base (code, text)
235 public class ErrorMessage : Message {
236 public ErrorMessage (int code, string text) : base (code, text)
241 // For compatibility reasons with old code.
243 public static void report_error (string error)
245 Console.Write ("ERROR: ");
246 Console.WriteLine (error);