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));
273 else if (arg is IntPtr)
274 sb.Append (String.Format ("IntPtr(0x{0:x})", ((IntPtr) arg).ToInt32 ()));
280 Console.WriteLine (sb.ToString ());
283 static public string PrintCollection (ICollection collection)
285 StringBuilder sb = new StringBuilder ();
287 sb.Append (collection.GetType ());
291 foreach (object o in collection) {
300 return sb.ToString ();
304 public class Message {
308 public Message (int code, string text)
315 public class WarningMessage : Message {
316 public WarningMessage (int code, string text) : base (code, text)
321 public class ErrorMessage : Message {
322 public ErrorMessage (int code, string text) : base (code, text)
327 // For compatibility reasons with old code.
329 public static void report_error (string error)
331 Console.Write ("ERROR: ");
332 Console.WriteLine (error);
336 public enum TimerType {
346 public enum CounterType {
355 static DateTime[] timer_start;
356 static TimeSpan[] timers;
357 static long[] timer_counters;
358 static long[] counters;
362 timer_start = new DateTime [(int) TimerType.CountTimers];
363 timers = new TimeSpan [(int) TimerType.CountTimers];
364 timer_counters = new long [(int) TimerType.CountTimers];
365 counters = new long [(int) CounterType.CountCounters];
367 for (int i = 0; i < (int) TimerType.CountTimers; i++) {
368 timer_start [i] = DateTime.Now;
369 timers [i] = TimeSpan.Zero;
373 [Conditional("TIMER")]
374 static public void IncrementCounter (CounterType which)
376 ++counters [(int) which];
379 [Conditional("TIMER")]
380 static public void StartTimer (TimerType which)
382 timer_start [(int) which] = DateTime.Now;
385 [Conditional("TIMER")]
386 static public void StopTimer (TimerType which)
388 timers [(int) which] += DateTime.Now - timer_start [(int) which];
389 ++timer_counters [(int) which];
392 [Conditional("TIMER")]
393 static public void ShowTimers ()
395 ShowTimer (TimerType.FindMembers, "- FindMembers timer");
396 ShowTimer (TimerType.TcFindMembers, "- TypeContainer.FindMembers timer");
397 ShowTimer (TimerType.MemberLookup, "- MemberLookup timer");
398 ShowTimer (TimerType.CachedLookup, "- CachedLookup timer");
399 ShowTimer (TimerType.CacheInit, "- Cache init");
400 ShowTimer (TimerType.MiscTimer, "- Misc timer");
402 ShowCounter (CounterType.FindMembers, "- Find members");
403 ShowCounter (CounterType.MemberCache, "- Member cache");
404 ShowCounter (CounterType.MiscCounter, "- Misc counter");
407 static public void ShowCounter (CounterType which, string msg)
409 Console.WriteLine ("{0} {1}", counters [(int) which], msg);
412 static public void ShowTimer (TimerType which, string msg)
415 "[{0:00}:{1:000}] {2} (used {3} times)",
416 (int) timers [(int) which].TotalSeconds,
417 timers [(int) which].Milliseconds, msg,
418 timer_counters [(int) which]);
422 public class InternalErrorException : Exception {
423 public InternalErrorException ()
424 : base ("Internal error")
428 public InternalErrorException (string message)
433 public InternalErrorException (string format, params object[] args)
434 : this (String.Format (format, args))