2004-11-07 Ben Maurer <bmaurer@ximian.com>
[mono-project.git] / mcs / mbas / report.cs
blob64d4863c4e73479394f8e75ac23165ccef7ba3cf
1 //
2 // report.cs: report errors and warnings.
3 //
4 // Author: Miguel de Icaza (miguel@ximian.com)
5 //
6 // (C) 2001 Ximian, Inc. (http://www.ximian.com)
7 //
9 //
10 // FIXME: currently our class library does not support custom number format strings
12 using System;
13 using System.Text;
14 using System.Collections;
15 using System.Diagnostics;
17 namespace Mono.MonoBASIC {
19 /// <summary>
20 /// This class is used to report errors and warnings t te user.
21 /// </summary>
22 public class Report {
23 /// <summary>
24 /// Errors encountered so far
25 /// </summary>
26 static public int Errors;
28 /// <summary>
29 /// Warnings encountered so far
30 /// </summary>
31 static public int Warnings;
33 /// <summary>
34 /// Whether errors should be throw an exception
35 /// </summary>
36 static public bool Fatal;
38 /// <summary>
39 /// Whether warnings should be considered errors
40 /// </summary>
41 static public bool WarningsAreErrors;
43 /// <summary>
44 /// Whether to dump a stack trace on errors.
45 /// </summary>
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)
63 if (!quiet)
65 if (Report.ExpectedError != 0)
66 Console.WriteLine("Failed to report expected Error " + Report.ExpectedError);
68 if (Errors == 0)
70 if (Warnings == 0)
71 Console.WriteLine("Compilation succeeded");
72 else
73 Console.WriteLine("Compilation succeeded: {0} warning(s)", Warnings);
75 else
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)
85 if (Fatal)
86 throw new Exception ();
88 Environment.Exit (0);
92 static private void RealError (string msg)
94 Errors++;
95 Console.WriteLine (msg);
97 if (Stacktrace)
98 Console.WriteLine (new StackTrace ().ToString ());
99 if (Fatal)
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);
108 RealError (msg);
109 Check (code);
112 static public void Warning (int code, Location l, string text)
114 if (warning_ignore_table != null){
115 if (warning_ignore_table.Contains (code))
116 return;
119 if (WarningsAreErrors)
120 Error (code, l, text);
121 else {
122 if (Location.IsNull (l))
123 Console.WriteLine(String.Format("{0} warning BC{1:0000}: {2}",
124 l.Name, code, text));
125 else
126 Console.WriteLine(String.Format("{0}({1},{2}) warning BC{3:0000}: {4}",
127 l.Name, l.Row, l.Col, code, text));
128 Warnings++;
129 Check (code);
131 if (Stacktrace)
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);
157 RealError (msg);
158 Check (code);
161 static public void Message (Message m)
163 if (m is ErrorMessage)
164 Error (m.code, m.text);
165 else
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 {
178 set {
179 expected_error = value;
181 get {
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)
198 return;
200 StringBuilder sb = new StringBuilder (message);
202 if ((args != null) && (args.Length > 0)) {
203 sb.Append (": ");
205 bool first = true;
206 foreach (object arg in args) {
207 if (first)
208 first = false;
209 else
210 sb.Append (",");
211 if (arg == null)
212 sb.Append ("null");
213 else if (arg is ICollection)
214 sb.Append (PrintCollection ((ICollection) arg));
215 else
216 sb.Append (arg);
220 Console.WriteLine (sb.ToString ());
223 static public string PrintCollection (ICollection collection)
225 StringBuilder sb = new StringBuilder ();
227 sb.Append (collection.GetType ());
228 sb.Append ("(");
230 bool first = true;
231 foreach (object o in collection) {
232 if (first)
233 first = false;
234 else
235 sb.Append (",");
236 sb.Append (o);
239 sb.Append (")");
240 return sb.ToString ();
244 public class Message {
245 public int code;
246 public string text;
248 public Message (int code, string text)
250 this.code = code;
251 this.text = 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 {
277 FindMembers = 0,
278 TcFindMembers = 1,
279 MemberLookup = 2,
280 CachedLookup = 3,
281 CacheInit = 4,
282 MiscTimer = 5,
283 CountTimers = 6
286 public enum CounterType {
287 FindMembers = 0,
288 MemberCache = 1,
289 MiscCounter = 2,
290 CountCounters = 3
293 public class Timer
295 static DateTime[] timer_start;
296 static TimeSpan[] timers;
297 static long[] timer_counters;
298 static long[] counters;
300 static Timer ()
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)
354 Console.WriteLine (
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)
369 : base (message)