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)
87 string msg = String.Format (
88 "{0}({1}) error CS{2:0000}: {3}", l.Name, l.Row, code, text);
89 // "{0}({1}) error CS{2}: {3}", l.Name, l.Row, code, text);
95 static public void Warning (int code, Location l, string text)
100 if (warning_ignore_table != null){
101 if (warning_ignore_table.Contains (code))
105 if (WarningsAreErrors)
106 Error (code, l, text);
110 if (Location.IsNull (l))
113 row = l.Row.ToString ();
115 Console.WriteLine (String.Format (
116 "{0}({1}) warning CS{2:0000}: {3}",
117 // "{0}({1}) warning CS{2}: {3}",
118 l.Name, row, code, text));
123 Console.WriteLine (new StackTrace ().ToString ());
127 static public void Warning (int code, string text)
129 Warning (code, Location.Null, text);
132 static public void Warning (int code, int level, string text)
134 if (RootContext.WarningLevel >= level)
135 Warning (code, Location.Null, text);
138 static public void Warning (int code, int level, Location l, string text)
140 if (RootContext.WarningLevel >= level)
141 Warning (code, l, text);
144 static public void Error (int code, string text)
149 string msg = String.Format ("error CS{0:0000}: {1}", code, text);
150 // string msg = String.Format ("error CS{0}: {1}", code, text);
156 static public void Message (Message m)
158 if (m is ErrorMessage)
159 Error (m.code, m.text);
161 Warning (m.code, m.text);
164 static public void SetIgnoreWarning (int code)
166 if (warning_ignore_table == null)
167 warning_ignore_table = new Hashtable ();
169 warning_ignore_table [code] = true;
172 static public int ExpectedError {
174 expected_error = value;
177 return expected_error;
181 public static int DebugFlags = 0;
183 [Conditional ("MCS_DEBUG")]
184 static public void Debug (string message, params object[] args)
186 Debug (4, message, args);
189 [Conditional ("MCS_DEBUG")]
190 static public void Debug (int category, string message, params object[] args)
192 if ((category & DebugFlags) == 0)
195 StringBuilder sb = new StringBuilder (message);
197 if ((args != null) && (args.Length > 0)) {
201 foreach (object arg in args) {
208 else if (arg is ICollection)
209 sb.Append (PrintCollection ((ICollection) arg));
215 Console.WriteLine (sb.ToString ());
218 static public string PrintCollection (ICollection collection)
220 StringBuilder sb = new StringBuilder ();
222 sb.Append (collection.GetType ());
226 foreach (object o in collection) {
235 return sb.ToString ();
239 public class Message {
243 public Message (int code, string text)
250 public class WarningMessage : Message {
251 public WarningMessage (int code, string text) : base (code, text)
256 public class ErrorMessage : Message {
257 public ErrorMessage (int code, string text) : base (code, text)
262 // For compatibility reasons with old code.
264 public static void report_error (string error)
266 Console.Write ("ERROR: ");
267 Console.WriteLine (error);
271 public enum TimerType {
281 public enum CounterType {
290 static DateTime[] timer_start;
291 static TimeSpan[] timers;
292 static long[] timer_counters;
293 static long[] counters;
297 timer_start = new DateTime [(int) TimerType.CountTimers];
298 timers = new TimeSpan [(int) TimerType.CountTimers];
299 timer_counters = new long [(int) TimerType.CountTimers];
300 counters = new long [(int) CounterType.CountCounters];
302 for (int i = 0; i < (int) TimerType.CountTimers; i++) {
303 timer_start [i] = DateTime.Now;
304 timers [i] = TimeSpan.Zero;
308 [Conditional("TIMER")]
309 static public void IncrementCounter (CounterType which)
311 ++counters [(int) which];
314 [Conditional("TIMER")]
315 static public void StartTimer (TimerType which)
317 timer_start [(int) which] = DateTime.Now;
320 [Conditional("TIMER")]
321 static public void StopTimer (TimerType which)
323 timers [(int) which] += DateTime.Now - timer_start [(int) which];
324 ++timer_counters [(int) which];
327 [Conditional("TIMER")]
328 static public void ShowTimers ()
330 ShowTimer (TimerType.FindMembers, "- FindMembers timer");
331 ShowTimer (TimerType.TcFindMembers, "- TypeContainer.FindMembers timer");
332 ShowTimer (TimerType.MemberLookup, "- MemberLookup timer");
333 ShowTimer (TimerType.CachedLookup, "- CachedLookup timer");
334 ShowTimer (TimerType.CacheInit, "- Cache init");
335 ShowTimer (TimerType.MiscTimer, "- Misc timer");
337 ShowCounter (CounterType.FindMembers, "- Find members");
338 ShowCounter (CounterType.MemberCache, "- Member cache");
339 ShowCounter (CounterType.MiscCounter, "- Misc counter");
342 static public void ShowCounter (CounterType which, string msg)
344 Console.WriteLine ("{0} {1}", counters [(int) which], msg);
347 static public void ShowTimer (TimerType which, string msg)
350 "[{0:00}:{1:000}] {2} (used {3} times)",
351 (int) timers [(int) which].TotalSeconds,
352 timers [(int) which].Milliseconds, msg,
353 timer_counters [(int) which]);
357 public class InternalErrorException : Exception {
358 public InternalErrorException ()
359 : base ("Internal error")
363 public InternalErrorException (string message)