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;
63 public WarningData (int level, string text) {
68 public bool IsEnabled ()
70 return RootContext.WarningLevel >= Level;
73 public string Format (params object[] args)
75 return String.Format (Message, args);
78 readonly string Message;
82 static string GetErrorMsg (int error_no)
85 case 3001: return "Argument type '{0}' is not CLS-compliant";
86 case 3002: return "Return type of '{0}' is not CLS-compliant";
87 case 3003: return "Type of '{0}' is not CLS-compliant";
88 case 3005: return "Identifier '{0}' differing only in case is not CLS-compliant";
89 case 3006: return "Overloaded method '{0}' differing only in ref or out, or in array rank, is not CLS-compliant";
90 case 3008: return "Identifier '{0}' is not CLS-compliant";
91 case 3009: return "'{0}': base type '{1}' is not CLS-compliant";
92 case 3010: return "'{0}': CLS-compliant interfaces must have only CLS-compliant members";
93 case 3011: return "'{0}': only CLS-compliant members can be abstract";
94 case 3013: return "Added modules must be marked with the CLSCompliant attribute to match the assembly";
95 case 3014: return "'{0}' cannot be marked as CLS-compliant because the assembly does not have a CLSCompliant attribute";
96 case 3015: return "'{0}' has no accessible constructors which use only CLS-compliant types";
97 case 3016: return "Arrays as attribute arguments are not CLS-compliant";
99 throw new InternalErrorException (String.Format ("Missing error '{0}' text", error_no));
102 static WarningData GetWarningMsg (int warn_no)
105 case 3012: return new WarningData (1, "You must specify the CLSCompliant attribute on the assembly, not the module, to enable CLS compliance checking");
108 throw new InternalErrorException (String.Format ("Wrong warning number '{0}'", warn_no));
111 static void Check (int code)
113 if (code == expected_error){
115 throw new Exception ();
117 Environment.Exit (0);
121 public static string FriendlyStackTrace (Exception e)
123 return FriendlyStackTrace (new StackTrace (e, true));
126 static string FriendlyStackTrace (StackTrace t)
128 StringBuilder sb = new StringBuilder ();
130 bool foundUserCode = false;
132 for (int i = 0; i < t.FrameCount; i++) {
133 StackFrame f = t.GetFrame (i);
134 MethodBase mb = f.GetMethod ();
136 if (!foundUserCode && mb.ReflectedType == typeof (Report))
139 foundUserCode = true;
143 if (f.GetFileLineNumber () > 0)
144 sb.AppendFormat ("(at {0}:{1}) ", f.GetFileName (), f.GetFileLineNumber ());
146 sb.AppendFormat ("{0}.{1} (", mb.ReflectedType.Name, mb.Name);
149 foreach (ParameterInfo pi in mb.GetParameters ()) {
154 sb.Append (TypeManager.CSharpName (pi.ParameterType));
159 return sb.ToString ();
162 static public void LocationOfPreviousError (Location loc) {
163 Console.WriteLine (String.Format ("{0}({1}) (Location of symbol related to previous error)", loc.Name, loc.Row));
166 static public void RealError (string msg)
169 Console.WriteLine (msg);
172 Console.WriteLine (FriendlyStackTrace (new StackTrace (true)));
175 throw new Exception (msg);
180 /// Method reports warning message. Only one reason why exist Warning and Report methods is beter code readability.
182 static public void Warning (int code, Location loc, params object[] args)
184 WarningData warning = GetWarningMsg (code);
185 if (!warning.IsEnabled ())
188 Warning (code, loc, warning.Format (args));
192 /// Reports error message.
194 static public void Error_T (int code, Location loc, params object[] args)
196 Error_T (code, String.Format ("{0}({1})", loc.Name, loc.Row), args);
199 static public void Error_T (int code, string location, params object[] args)
201 string errorText = String.Format (GetErrorMsg (code), args);
202 PrintError (code, location, errorText);
205 static void PrintError (int code, string l, string text)
210 string msg = String.Format ("{0} error CS{1:0000}: {2}", l, code, text);
215 static public void Error (int code, Location l, string text)
220 string msg = String.Format (
221 "{0}({1}) error CS{2:0000}: {3}", l.Name, l.Row, code, text);
222 // "{0}({1}) error CS{2}: {3}", l.Name, l.Row, code, text);
228 static public void Warning (int code, Location l, string text)
233 if (warning_ignore_table != null){
234 if (warning_ignore_table.Contains (code))
238 if (WarningsAreErrors)
239 Error (code, l, text);
243 if (Location.IsNull (l))
246 row = l.Row.ToString ();
248 Console.WriteLine (String.Format (
249 "{0}({1}) warning CS{2:0000}: {3}",
250 // "{0}({1}) warning CS{2}: {3}",
251 l.Name, row, code, text));
256 Console.WriteLine (new StackTrace ().ToString ());
260 static public void Warning (int code, string text)
262 Warning (code, Location.Null, text);
265 static public void Warning (int code, int level, string text)
267 if (RootContext.WarningLevel >= level)
268 Warning (code, Location.Null, text);
271 static public void Warning (int code, int level, Location l, string text)
273 if (RootContext.WarningLevel >= level)
274 Warning (code, l, text);
277 static public void Error (int code, string text)
282 string msg = String.Format ("error CS{0:0000}: {1}", code, text);
283 // string msg = String.Format ("error CS{0}: {1}", code, text);
289 static public void Error (int code, Location loc, string format, params object[] args)
291 Error (code, loc, String.Format (format, args));
294 static public void Warning (int code, Location loc, string format, params object[] args)
296 Warning (code, loc, String.Format (format, args));
299 static public void Warning (int code, string format, params object[] args)
301 Warning (code, String.Format (format, args));
304 static public void Message (Message m)
306 if (m is ErrorMessage)
307 Error (m.code, m.text);
309 Warning (m.code, m.text);
312 static public void SetIgnoreWarning (int code)
314 if (warning_ignore_table == null)
315 warning_ignore_table = new Hashtable ();
317 warning_ignore_table [code] = true;
320 static public int ExpectedError {
322 expected_error = value;
325 return expected_error;
329 public static int DebugFlags = 0;
331 [Conditional ("MCS_DEBUG")]
332 static public void Debug (string message, params object[] args)
334 Debug (4, message, args);
337 [Conditional ("MCS_DEBUG")]
338 static public void Debug (int category, string message, params object[] args)
340 if ((category & DebugFlags) == 0)
343 StringBuilder sb = new StringBuilder (message);
345 if ((args != null) && (args.Length > 0)) {
349 foreach (object arg in args) {
356 else if (arg is ICollection)
357 sb.Append (PrintCollection ((ICollection) arg));
358 else if (arg is IntPtr)
359 sb.Append (String.Format ("IntPtr(0x{0:x})", ((IntPtr) arg).ToInt32 ()));
365 Console.WriteLine (sb.ToString ());
368 static public string PrintCollection (ICollection collection)
370 StringBuilder sb = new StringBuilder ();
372 sb.Append (collection.GetType ());
376 foreach (object o in collection) {
385 return sb.ToString ();
389 public class Message {
393 public Message (int code, string text)
400 public class WarningMessage : Message {
401 public WarningMessage (int code, string text) : base (code, text)
406 public class ErrorMessage : Message {
407 public ErrorMessage (int code, string text) : base (code, text)
412 // For compatibility reasons with old code.
414 public static void report_error (string error)
416 Console.Write ("ERROR: ");
417 Console.WriteLine (error);
421 public enum TimerType {
431 public enum CounterType {
440 static DateTime[] timer_start;
441 static TimeSpan[] timers;
442 static long[] timer_counters;
443 static long[] counters;
447 timer_start = new DateTime [(int) TimerType.CountTimers];
448 timers = new TimeSpan [(int) TimerType.CountTimers];
449 timer_counters = new long [(int) TimerType.CountTimers];
450 counters = new long [(int) CounterType.CountCounters];
452 for (int i = 0; i < (int) TimerType.CountTimers; i++) {
453 timer_start [i] = DateTime.Now;
454 timers [i] = TimeSpan.Zero;
458 [Conditional("TIMER")]
459 static public void IncrementCounter (CounterType which)
461 ++counters [(int) which];
464 [Conditional("TIMER")]
465 static public void StartTimer (TimerType which)
467 timer_start [(int) which] = DateTime.Now;
470 [Conditional("TIMER")]
471 static public void StopTimer (TimerType which)
473 timers [(int) which] += DateTime.Now - timer_start [(int) which];
474 ++timer_counters [(int) which];
477 [Conditional("TIMER")]
478 static public void ShowTimers ()
480 ShowTimer (TimerType.FindMembers, "- FindMembers timer");
481 ShowTimer (TimerType.TcFindMembers, "- TypeContainer.FindMembers timer");
482 ShowTimer (TimerType.MemberLookup, "- MemberLookup timer");
483 ShowTimer (TimerType.CachedLookup, "- CachedLookup timer");
484 ShowTimer (TimerType.CacheInit, "- Cache init");
485 ShowTimer (TimerType.MiscTimer, "- Misc timer");
487 ShowCounter (CounterType.FindMembers, "- Find members");
488 ShowCounter (CounterType.MemberCache, "- Member cache");
489 ShowCounter (CounterType.MiscCounter, "- Misc counter");
492 static public void ShowCounter (CounterType which, string msg)
494 Console.WriteLine ("{0} {1}", counters [(int) which], msg);
497 static public void ShowTimer (TimerType which, string msg)
500 "[{0:00}:{1:000}] {2} (used {3} times)",
501 (int) timers [(int) which].TotalSeconds,
502 timers [(int) which].Milliseconds, msg,
503 timer_counters [(int) which]);
507 public class InternalErrorException : Exception {
508 public InternalErrorException ()
509 : base ("Internal error")
513 public InternalErrorException (string message)
518 public InternalErrorException (string format, params object[] args)
519 : this (String.Format (format, args))