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 Error (int code, Location loc, string format, params object[] args)
158 Error (code, loc, String.Format (format, args));
161 static public void Error (int code, string format, params object[] args)
163 Error (code, String.Format (format, args));
166 static public void Warning (int code, Location loc, string format, params object[] args)
168 Warning (code, loc, String.Format (format, args));
171 static public void Warning (int code, string format, params object[] args)
173 Warning (code, String.Format (format, args));
176 static public void Message (Message m)
178 if (m is ErrorMessage)
179 Error (m.code, m.text);
181 Warning (m.code, m.text);
184 static public void SetIgnoreWarning (int code)
186 if (warning_ignore_table == null)
187 warning_ignore_table = new Hashtable ();
189 warning_ignore_table [code] = true;
192 static public int ExpectedError {
194 expected_error = value;
197 return expected_error;
201 public static int DebugFlags = 0;
203 [Conditional ("MCS_DEBUG")]
204 static public void Debug (string message, params object[] args)
206 Debug (4, message, args);
209 [Conditional ("MCS_DEBUG")]
210 static public void Debug (int category, string message, params object[] args)
212 if ((category & DebugFlags) == 0)
215 StringBuilder sb = new StringBuilder (message);
217 if ((args != null) && (args.Length > 0)) {
221 foreach (object arg in args) {
228 else if (arg is ICollection)
229 sb.Append (PrintCollection ((ICollection) arg));
235 Console.WriteLine (sb.ToString ());
238 static public string PrintCollection (ICollection collection)
240 StringBuilder sb = new StringBuilder ();
242 sb.Append (collection.GetType ());
246 foreach (object o in collection) {
255 return sb.ToString ();
259 public class Message {
263 public Message (int code, string text)
270 public class WarningMessage : Message {
271 public WarningMessage (int code, string text) : base (code, text)
276 public class ErrorMessage : Message {
277 public ErrorMessage (int code, string text) : base (code, text)
282 // For compatibility reasons with old code.
284 public static void report_error (string error)
286 Console.Write ("ERROR: ");
287 Console.WriteLine (error);
291 public enum TimerType {
301 public enum CounterType {
310 static DateTime[] timer_start;
311 static TimeSpan[] timers;
312 static long[] timer_counters;
313 static long[] counters;
317 timer_start = new DateTime [(int) TimerType.CountTimers];
318 timers = new TimeSpan [(int) TimerType.CountTimers];
319 timer_counters = new long [(int) TimerType.CountTimers];
320 counters = new long [(int) CounterType.CountCounters];
322 for (int i = 0; i < (int) TimerType.CountTimers; i++) {
323 timer_start [i] = DateTime.Now;
324 timers [i] = TimeSpan.Zero;
328 [Conditional("TIMER")]
329 static public void IncrementCounter (CounterType which)
331 ++counters [(int) which];
334 [Conditional("TIMER")]
335 static public void StartTimer (TimerType which)
337 timer_start [(int) which] = DateTime.Now;
340 [Conditional("TIMER")]
341 static public void StopTimer (TimerType which)
343 timers [(int) which] += DateTime.Now - timer_start [(int) which];
344 ++timer_counters [(int) which];
347 [Conditional("TIMER")]
348 static public void ShowTimers ()
350 ShowTimer (TimerType.FindMembers, "- FindMembers timer");
351 ShowTimer (TimerType.TcFindMembers, "- TypeContainer.FindMembers timer");
352 ShowTimer (TimerType.MemberLookup, "- MemberLookup timer");
353 ShowTimer (TimerType.CachedLookup, "- CachedLookup timer");
354 ShowTimer (TimerType.CacheInit, "- Cache init");
355 ShowTimer (TimerType.MiscTimer, "- Misc timer");
357 ShowCounter (CounterType.FindMembers, "- Find members");
358 ShowCounter (CounterType.MemberCache, "- Member cache");
359 ShowCounter (CounterType.MiscCounter, "- Misc counter");
362 static public void ShowCounter (CounterType which, string msg)
364 Console.WriteLine ("{0} {1}", counters [(int) which], msg);
367 static public void ShowTimer (TimerType which, string msg)
370 "[{0:00}:{1:000}] {2} (used {3} times)",
371 (int) timers [(int) which].TotalSeconds,
372 timers [(int) which].Milliseconds, msg,
373 timer_counters [(int) which]);
377 public class InternalErrorException : Exception {
378 public InternalErrorException ()
379 : base ("Internal error")
383 public InternalErrorException (string message)