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 public int ProcessResults(bool quiet)
65 if (Report.ExpectedError != 0)
66 Console.WriteLine("Failed to report expected Error " + Report.ExpectedError);
71 Console.WriteLine("Compilation succeeded");
73 Console.WriteLine("Compilation succeeded: {0} warning(s)", Warnings);
76 Console.WriteLine("Compilation failed: {0} Error(s), {1} warnings", Errors, Warnings);
78 return (Errors == 0)?0:1;
81 static void Check (int code)
83 if (code == expected_error)
86 throw new Exception ();
92 static private void RealError (string msg)
95 Console.WriteLine (msg);
98 Console.WriteLine (new StackTrace ().ToString ());
100 throw new Exception (msg);
103 static public void Error (int code, Location l, string text)
105 string msg = String.Format (
106 "{0}({1},{2}) error BC{3:0000}: {4}", l.Name, l.Row, l.Col, code, text);
112 static public void Warning (int code, Location l, string text)
114 if (warning_ignore_table != null){
115 if (warning_ignore_table.Contains (code))
119 if (WarningsAreErrors)
120 Error (code, l, text);
124 if (Location.IsNull (l))
125 Console.WriteLine(String.Format("{0} warning BC{2:0000}: {3}",
126 l.Name, code, text));
128 Console.WriteLine(String.Format("{0}({1},{2}) warning BC{2:0000}: {3}",
129 l.Name, l.Row, l.Col, code, text));
134 Console.WriteLine (new StackTrace ().ToString ());
138 static public void Warning (int code, string text)
140 Warning (code, Location.Null, text);
143 static public void Warning (int code, int level, string text)
145 if (RootContext.WarningLevel >= level)
146 Warning (code, Location.Null, text);
149 static public void Warning (int code, int level, Location l, string text)
151 if (RootContext.WarningLevel >= level)
152 Warning (code, l, text);
155 static public void Error (int code, string text)
157 string msg = String.Format ("error BC{0:0000}: {1}", code, text);
163 static public void Message (Message m)
165 if (m is ErrorMessage)
166 Error (m.code, m.text);
168 Warning (m.code, m.text);
171 static public void SetIgnoreWarning (int code)
173 if (warning_ignore_table == null)
174 warning_ignore_table = new Hashtable ();
176 warning_ignore_table [code] = true;
179 static public int ExpectedError {
181 expected_error = value;
184 return expected_error;
188 public static int DebugFlags = 0;
190 [Conditional ("MCS_DEBUG")]
191 static public void Debug (string message, params object[] args)
193 Debug (4, message, args);
196 [Conditional ("MCS_DEBUG")]
197 static public void Debug (int category, string message, params object[] args)
199 if ((category & DebugFlags) == 0)
202 StringBuilder sb = new StringBuilder (message);
204 if ((args != null) && (args.Length > 0)) {
208 foreach (object arg in args) {
215 else if (arg is ICollection)
216 sb.Append (PrintCollection ((ICollection) arg));
222 Console.WriteLine (sb.ToString ());
225 static public string PrintCollection (ICollection collection)
227 StringBuilder sb = new StringBuilder ();
229 sb.Append (collection.GetType ());
233 foreach (object o in collection) {
242 return sb.ToString ();
246 public class Message {
250 public Message (int code, string text)
257 public class WarningMessage : Message {
258 public WarningMessage (int code, string text) : base (code, text)
263 public class ErrorMessage : Message {
264 public ErrorMessage (int code, string text) : base (code, text)
269 // For compatibility reasons with old code.
271 public static void report_error (string error)
273 Console.Write ("ERROR: ");
274 Console.WriteLine (error);
278 public enum TimerType {
288 public enum CounterType {
297 static DateTime[] timer_start;
298 static TimeSpan[] timers;
299 static long[] timer_counters;
300 static long[] counters;
304 timer_start = new DateTime [(int) TimerType.CountTimers];
305 timers = new TimeSpan [(int) TimerType.CountTimers];
306 timer_counters = new long [(int) TimerType.CountTimers];
307 counters = new long [(int) CounterType.CountCounters];
309 for (int i = 0; i < (int) TimerType.CountTimers; i++) {
310 timer_start [i] = DateTime.Now;
311 timers [i] = TimeSpan.Zero;
315 [Conditional("TIMER")]
316 static public void IncrementCounter (CounterType which)
318 ++counters [(int) which];
321 [Conditional("TIMER")]
322 static public void StartTimer (TimerType which)
324 timer_start [(int) which] = DateTime.Now;
327 [Conditional("TIMER")]
328 static public void StopTimer (TimerType which)
330 timers [(int) which] += DateTime.Now - timer_start [(int) which];
331 ++timer_counters [(int) which];
334 [Conditional("TIMER")]
335 static public void ShowTimers ()
337 ShowTimer (TimerType.FindMembers, "- FindMembers timer");
338 ShowTimer (TimerType.TcFindMembers, "- TypeContainer.FindMembers timer");
339 ShowTimer (TimerType.MemberLookup, "- MemberLookup timer");
340 ShowTimer (TimerType.CachedLookup, "- CachedLookup timer");
341 ShowTimer (TimerType.CacheInit, "- Cache init");
342 ShowTimer (TimerType.MiscTimer, "- Misc timer");
344 ShowCounter (CounterType.FindMembers, "- Find members");
345 ShowCounter (CounterType.MemberCache, "- Member cache");
346 ShowCounter (CounterType.MiscCounter, "- Misc counter");
349 static public void ShowCounter (CounterType which, string msg)
351 Console.WriteLine ("{0} {1}", counters [(int) which], msg);
354 static public void ShowTimer (TimerType which, string msg)
357 "[{0:00}:{1:000}] {2} (used {3} times)",
358 (int) timers [(int) which].TotalSeconds,
359 timers [(int) which].Milliseconds, msg,
360 timer_counters [(int) which]);
364 public class InternalErrorException : Exception {
365 public InternalErrorException ()
366 : base ("Internal error")
370 public InternalErrorException (string message)