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;
16 using System.Reflection;
18 namespace Mono.CSharp {
21 /// This class is used to report errors and warnings t te user.
25 /// Errors encountered so far
27 static public int Errors;
30 /// Warnings encountered so far
32 static public int Warnings;
35 /// Whether errors should be throw an exception
37 static public bool Fatal;
40 /// Whether warnings should be considered errors
42 static public bool WarningsAreErrors;
45 /// Whether to dump a stack trace on errors.
47 static public bool Stacktrace;
50 // If the 'expected' error code is reported then the
51 // compilation succeeds.
53 // Used for the test suite to excercise the error codes
55 static int expected_error = 0;
58 // Keeps track of the warnings that we are ignoring
60 static Hashtable warning_ignore_table;
62 static void Check (int code)
64 if (code == expected_error){
66 throw new Exception ();
72 public static string FriendlyStackTrace (Exception e)
74 return FriendlyStackTrace (new StackTrace (e, true));
77 static string FriendlyStackTrace (StackTrace t)
79 StringBuilder sb = new StringBuilder ();
81 bool foundUserCode = false;
83 for (int i = 0; i < t.FrameCount; i++) {
84 StackFrame f = t.GetFrame (i);
85 MethodBase mb = f.GetMethod ();
87 if (!foundUserCode && mb.ReflectedType == typeof (Report))
94 if (f.GetFileLineNumber () > 0)
95 sb.AppendFormat ("(at {0}:{1}) ", f.GetFileName (), f.GetFileLineNumber ());
97 sb.AppendFormat ("{0}.{1} (", mb.ReflectedType.Name, mb.Name);
100 foreach (ParameterInfo pi in mb.GetParameters ()) {
105 sb.Append (TypeManager.CSharpName (pi.ParameterType));
110 return sb.ToString ();
113 static public void RealError (string msg)
116 Console.WriteLine (msg);
119 Console.WriteLine (FriendlyStackTrace (new StackTrace (true)));
122 throw new Exception (msg);
125 static public void Error (int code, Location l, string text)
130 string msg = String.Format (
131 "{0}({1}) error CS{2:0000}: {3}", l.Name, l.Row, code, text);
132 // "{0}({1}) error CS{2}: {3}", l.Name, l.Row, code, text);
138 static public void Warning (int code, Location l, string text)
143 if (warning_ignore_table != null){
144 if (warning_ignore_table.Contains (code))
148 if (WarningsAreErrors)
149 Error (code, l, text);
153 if (Location.IsNull (l))
156 row = l.Row.ToString ();
158 Console.WriteLine (String.Format (
159 "{0}({1}) warning CS{2:0000}: {3}",
160 // "{0}({1}) warning CS{2}: {3}",
161 l.Name, row, code, text));
166 Console.WriteLine (new StackTrace ().ToString ());
170 static public void Warning (int code, string text)
172 Warning (code, Location.Null, text);
175 static public void Warning (int code, int level, string text)
177 if (RootContext.WarningLevel >= level)
178 Warning (code, Location.Null, text);
181 static public void Warning (int code, int level, Location l, string text)
183 if (RootContext.WarningLevel >= level)
184 Warning (code, l, text);
187 static public void Error (int code, string text)
192 string msg = String.Format ("error CS{0:0000}: {1}", code, text);
193 // string msg = String.Format ("error CS{0}: {1}", code, text);
199 static public void Error (int code, Location loc, string format, params object[] args)
201 Error (code, loc, String.Format (format, args));
204 static public void Error (int code, string format, params object[] args)
206 Error (code, String.Format (format, args));
209 static public void Warning (int code, Location loc, string format, params object[] args)
211 Warning (code, loc, String.Format (format, args));
214 static public void Warning (int code, string format, params object[] args)
216 Warning (code, String.Format (format, args));
219 static public void Message (Message m)
221 if (m is ErrorMessage)
222 Error (m.code, m.text);
224 Warning (m.code, m.text);
227 static public void SetIgnoreWarning (int code)
229 if (warning_ignore_table == null)
230 warning_ignore_table = new Hashtable ();
232 warning_ignore_table [code] = true;
235 static public int ExpectedError {
237 expected_error = value;
240 return expected_error;
244 public static int DebugFlags = 0;
246 [Conditional ("MCS_DEBUG")]
247 static public void Debug (string message, params object[] args)
249 Debug (4, message, args);
252 [Conditional ("MCS_DEBUG")]
253 static public void Debug (int category, string message, params object[] args)
255 if ((category & DebugFlags) == 0)
258 StringBuilder sb = new StringBuilder (message);
260 if ((args != null) && (args.Length > 0)) {
264 foreach (object arg in args) {
271 else if (arg is ICollection)
272 sb.Append (PrintCollection ((ICollection) arg));
278 Console.WriteLine (sb.ToString ());
281 static public string PrintCollection (ICollection collection)
283 StringBuilder sb = new StringBuilder ();
285 sb.Append (collection.GetType ());
289 foreach (object o in collection) {
298 return sb.ToString ();
302 public class Message {
306 public Message (int code, string text)
313 public class WarningMessage : Message {
314 public WarningMessage (int code, string text) : base (code, text)
319 public class ErrorMessage : Message {
320 public ErrorMessage (int code, string text) : base (code, text)
325 // For compatibility reasons with old code.
327 public static void report_error (string error)
329 Console.Write ("ERROR: ");
330 Console.WriteLine (error);
334 public enum TimerType {
344 public enum CounterType {
353 static DateTime[] timer_start;
354 static TimeSpan[] timers;
355 static long[] timer_counters;
356 static long[] counters;
360 timer_start = new DateTime [(int) TimerType.CountTimers];
361 timers = new TimeSpan [(int) TimerType.CountTimers];
362 timer_counters = new long [(int) TimerType.CountTimers];
363 counters = new long [(int) CounterType.CountCounters];
365 for (int i = 0; i < (int) TimerType.CountTimers; i++) {
366 timer_start [i] = DateTime.Now;
367 timers [i] = TimeSpan.Zero;
371 [Conditional("TIMER")]
372 static public void IncrementCounter (CounterType which)
374 ++counters [(int) which];
377 [Conditional("TIMER")]
378 static public void StartTimer (TimerType which)
380 timer_start [(int) which] = DateTime.Now;
383 [Conditional("TIMER")]
384 static public void StopTimer (TimerType which)
386 timers [(int) which] += DateTime.Now - timer_start [(int) which];
387 ++timer_counters [(int) which];
390 [Conditional("TIMER")]
391 static public void ShowTimers ()
393 ShowTimer (TimerType.FindMembers, "- FindMembers timer");
394 ShowTimer (TimerType.TcFindMembers, "- TypeContainer.FindMembers timer");
395 ShowTimer (TimerType.MemberLookup, "- MemberLookup timer");
396 ShowTimer (TimerType.CachedLookup, "- CachedLookup timer");
397 ShowTimer (TimerType.CacheInit, "- Cache init");
398 ShowTimer (TimerType.MiscTimer, "- Misc timer");
400 ShowCounter (CounterType.FindMembers, "- Find members");
401 ShowCounter (CounterType.MemberCache, "- Member cache");
402 ShowCounter (CounterType.MiscCounter, "- Misc counter");
405 static public void ShowCounter (CounterType which, string msg)
407 Console.WriteLine ("{0} {1}", counters [(int) which], msg);
410 static public void ShowTimer (TimerType which, string msg)
413 "[{0:00}:{1:000}] {2} (used {3} times)",
414 (int) timers [(int) which].TotalSeconds,
415 timers [(int) which].Milliseconds, msg,
416 timer_counters [(int) which]);
420 public class InternalErrorException : Exception {
421 public InternalErrorException ()
422 : base ("Internal error")
426 public InternalErrorException (string message)