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.MonoBASIC {
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);
122 if (Location.IsNull (l))
123 Console.WriteLine(String.Format("{0} warning BC{1:0000}: {2}",
124 l.Name, code, text));
126 Console.WriteLine(String.Format("{0}({1},{2}) warning BC{3:0000}: {4}",
127 l.Name, l.Row, l.Col, code, text));
132 Console.WriteLine (new StackTrace ().ToString ());
136 static public void Warning (int code, string text)
138 Warning (code, Location.Null, text);
141 static public void Warning (int code, int level, string text)
143 if (RootContext.WarningLevel >= level)
144 Warning (code, Location.Null, text);
147 static public void Warning (int code, int level, Location l, string text)
149 if (RootContext.WarningLevel >= level)
150 Warning (code, l, text);
153 static public void Error (int code, string text)
155 string msg = String.Format ("error BC{0:0000}: {1}", code, text);
161 static public void Message (Message m)
163 if (m is ErrorMessage)
164 Error (m.code, m.text);
166 Warning (m.code, m.text);
169 static public void SetIgnoreWarning (int code)
171 if (warning_ignore_table == null)
172 warning_ignore_table = new Hashtable ();
174 warning_ignore_table [code] = true;
177 static public int ExpectedError {
179 expected_error = value;
182 return expected_error;
186 public static int DebugFlags = 0;
188 [Conditional ("MCS_DEBUG")]
189 static public void Debug (string message, params object[] args)
191 Debug (4, message, args);
194 [Conditional ("MCS_DEBUG")]
195 static public void Debug (int category, string message, params object[] args)
197 if ((category & DebugFlags) == 0)
200 StringBuilder sb = new StringBuilder (message);
202 if ((args != null) && (args.Length > 0)) {
206 foreach (object arg in args) {
213 else if (arg is ICollection)
214 sb.Append (PrintCollection ((ICollection) arg));
220 Console.WriteLine (sb.ToString ());
223 static public string PrintCollection (ICollection collection)
225 StringBuilder sb = new StringBuilder ();
227 sb.Append (collection.GetType ());
231 foreach (object o in collection) {
240 return sb.ToString ();
244 public class Message {
248 public Message (int code, string text)
255 public class WarningMessage : Message {
256 public WarningMessage (int code, string text) : base (code, text)
261 public class ErrorMessage : Message {
262 public ErrorMessage (int code, string text) : base (code, text)
267 // For compatibility reasons with old code.
269 public static void report_error (string error)
271 Console.Write ("ERROR: ");
272 Console.WriteLine (error);
276 public enum TimerType {
286 public enum CounterType {
295 static DateTime[] timer_start;
296 static TimeSpan[] timers;
297 static long[] timer_counters;
298 static long[] counters;
302 timer_start = new DateTime [(int) TimerType.CountTimers];
303 timers = new TimeSpan [(int) TimerType.CountTimers];
304 timer_counters = new long [(int) TimerType.CountTimers];
305 counters = new long [(int) CounterType.CountCounters];
307 for (int i = 0; i < (int) TimerType.CountTimers; i++) {
308 timer_start [i] = DateTime.Now;
309 timers [i] = TimeSpan.Zero;
313 [Conditional("TIMER")]
314 static public void IncrementCounter (CounterType which)
316 ++counters [(int) which];
319 [Conditional("TIMER")]
320 static public void StartTimer (TimerType which)
322 timer_start [(int) which] = DateTime.Now;
325 [Conditional("TIMER")]
326 static public void StopTimer (TimerType which)
328 timers [(int) which] += DateTime.Now - timer_start [(int) which];
329 ++timer_counters [(int) which];
332 [Conditional("TIMER")]
333 static public void ShowTimers ()
335 ShowTimer (TimerType.FindMembers, "- FindMembers timer");
336 ShowTimer (TimerType.TcFindMembers, "- TypeContainer.FindMembers timer");
337 ShowTimer (TimerType.MemberLookup, "- MemberLookup timer");
338 ShowTimer (TimerType.CachedLookup, "- CachedLookup timer");
339 ShowTimer (TimerType.CacheInit, "- Cache init");
340 ShowTimer (TimerType.MiscTimer, "- Misc timer");
342 ShowCounter (CounterType.FindMembers, "- Find members");
343 ShowCounter (CounterType.MemberCache, "- Member cache");
344 ShowCounter (CounterType.MiscCounter, "- Misc counter");
347 static public void ShowCounter (CounterType which, string msg)
349 Console.WriteLine ("{0} {1}", counters [(int) which], msg);
352 static public void ShowTimer (TimerType which, string msg)
355 "[{0:00}:{1:000}] {2} (used {3} times)",
356 (int) timers [(int) which].TotalSeconds,
357 timers [(int) which].Milliseconds, msg,
358 timer_counters [(int) which]);
362 public class InternalErrorException : Exception {
363 public InternalErrorException ()
364 : base ("Internal error")
368 public InternalErrorException (string message)