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 exercise 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)
66 throw new Exception ();
72 static public void RealError (string msg)
75 Console.WriteLine (msg);
78 Console.WriteLine (new StackTrace().ToString ());
80 throw new Exception (msg);
83 static public void Error (int code, Location l, string text)
85 string msg = String.Format (
86 "{0}({1},{2}) error BC{3:0000}: {4}", l.Name, l.Row, l.Col, 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 BC{2:0000}: {3}",
111 // "{0}({1}) warning BC{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 Warning (int code, int level, string text)
128 if (RootContext.WarningLevel >= level)
129 Warning (code, Location.Null, text);
132 static public void Warning (int code, int level, Location l, string text)
134 if (RootContext.WarningLevel >= level)
135 Warning (code, l, text);
138 static public void Error (int code, string text)
140 string msg = String.Format ("error BC{0:0000}: {1}", code, text);
141 // string msg = String.Format ("error BC{0}: {1}", code, text);
147 static public void Message (Message m)
149 if (m is ErrorMessage)
150 Error (m.code, m.text);
152 Warning (m.code, m.text);
155 static public void SetIgnoreWarning (int code)
157 if (warning_ignore_table == null)
158 warning_ignore_table = new Hashtable ();
160 warning_ignore_table [code] = true;
163 static public int ExpectedError {
165 expected_error = value;
168 return expected_error;
172 public static int DebugFlags = 0;
174 [Conditional ("MCS_DEBUG")]
175 static public void Debug (string message, params object[] args)
177 Debug (4, message, args);
180 [Conditional ("MCS_DEBUG")]
181 static public void Debug (int category, string message, params object[] args)
183 if ((category & DebugFlags) == 0)
186 StringBuilder sb = new StringBuilder (message);
188 if ((args != null) && (args.Length > 0)) {
192 foreach (object arg in args) {
199 else if (arg is ICollection)
200 sb.Append (PrintCollection ((ICollection) arg));
206 Console.WriteLine (sb.ToString ());
209 static public string PrintCollection (ICollection collection)
211 StringBuilder sb = new StringBuilder ();
213 sb.Append (collection.GetType ());
217 foreach (object o in collection) {
226 return sb.ToString ();
230 public class Message {
234 public Message (int code, string text)
241 public class WarningMessage : Message {
242 public WarningMessage (int code, string text) : base (code, text)
247 public class ErrorMessage : Message {
248 public ErrorMessage (int code, string text) : base (code, text)
253 // For compatibility reasons with old code.
255 public static void report_error (string error)
257 Console.Write ("ERROR: ");
258 Console.WriteLine (error);
262 public enum TimerType {
272 public enum CounterType {
281 static DateTime[] timer_start;
282 static TimeSpan[] timers;
283 static long[] timer_counters;
284 static long[] counters;
288 timer_start = new DateTime [(int) TimerType.CountTimers];
289 timers = new TimeSpan [(int) TimerType.CountTimers];
290 timer_counters = new long [(int) TimerType.CountTimers];
291 counters = new long [(int) CounterType.CountCounters];
293 for (int i = 0; i < (int) TimerType.CountTimers; i++) {
294 timer_start [i] = DateTime.Now;
295 timers [i] = TimeSpan.Zero;
299 [Conditional("TIMER")]
300 static public void IncrementCounter (CounterType which)
302 ++counters [(int) which];
305 [Conditional("TIMER")]
306 static public void StartTimer (TimerType which)
308 timer_start [(int) which] = DateTime.Now;
311 [Conditional("TIMER")]
312 static public void StopTimer (TimerType which)
314 timers [(int) which] += DateTime.Now - timer_start [(int) which];
315 ++timer_counters [(int) which];
318 [Conditional("TIMER")]
319 static public void ShowTimers ()
321 ShowTimer (TimerType.FindMembers, "- FindMembers timer");
322 ShowTimer (TimerType.TcFindMembers, "- TypeContainer.FindMembers timer");
323 ShowTimer (TimerType.MemberLookup, "- MemberLookup timer");
324 ShowTimer (TimerType.CachedLookup, "- CachedLookup timer");
325 ShowTimer (TimerType.CacheInit, "- Cache init");
326 ShowTimer (TimerType.MiscTimer, "- Misc timer");
328 ShowCounter (CounterType.FindMembers, "- Find members");
329 ShowCounter (CounterType.MemberCache, "- Member cache");
330 ShowCounter (CounterType.MiscCounter, "- Misc counter");
333 static public void ShowCounter (CounterType which, string msg)
335 Console.WriteLine ("{0} {1}", counters [(int) which], msg);
338 static public void ShowTimer (TimerType which, string msg)
341 "[{0:00}:{1:000}] {2} (used {3} times)",
342 (int) timers [(int) which].TotalSeconds,
343 timers [(int) which].Milliseconds, msg,
344 timer_counters [(int) which]);
348 public class InternalErrorException : Exception {
349 public InternalErrorException ()
350 : base ("Internal error")
354 public InternalErrorException (string message)