2 // report.cs: report errors and warnings.
4 // Author: Miguel de Icaza (miguel@ximian.com)
5 // Marek Safar (marek.safar@seznam.cz)
7 // (C) 2001 Ximian, Inc. (http://www.ximian.com)
13 using System.Collections;
14 using System.Collections.Specialized;
15 using System.Diagnostics;
16 using System.Reflection;
17 using System.Reflection.Emit;
19 namespace Mono.CSharp {
22 /// This class is used to report errors and warnings t te user.
26 /// Errors encountered so far
28 static public int Errors;
31 /// Warnings encountered so far
33 static public int Warnings;
36 /// Whether errors should be throw an exception
38 static public bool Fatal;
41 /// Whether warnings should be considered errors
43 static public bool WarningsAreErrors;
46 /// Whether to dump a stack trace on errors.
48 static public bool Stacktrace;
50 static public TextWriter Stderr = Console.Error;
53 // If the 'expected' error code is reported then the
54 // compilation succeeds.
56 // Used for the test suite to excercise the error codes
58 static int expected_error = 0;
61 // Keeps track of the warnings that we are ignoring
63 public static Hashtable warning_ignore_table;
65 static Hashtable warning_regions_table;
68 /// List of symbols related to reported error/warning. You have to fill it before error/warning is reported.
70 static StringCollection extra_information = new StringCollection ();
73 // IF YOU ADD A NEW WARNING YOU HAVE TO ADD ITS ID HERE
75 public static readonly int[] AllWarnings = new int[] {
77 105, 108, 109, 114, 162, 164, 168, 169, 183, 184, 197,
78 219, 251, 252, 253, 282,
79 419, 420, 429, 436, 440, 465, 467, 469,
80 612, 618, 626, 628, 642, 649, 652, 658, 659, 660, 661, 665, 672,
82 1522, 1570, 1571, 1572, 1573, 1574, 1580, 1581, 1584, 1587, 1589, 1590, 1591, 1592,
83 1616, 1633, 1634, 1635, 1690, 1691, 1692,
87 3005, 3012, 3019, 3021, 3022, 3023, 3026, 3027,
95 // Just to be sure that binary search is working
96 Array.Sort (AllWarnings);
99 public static void Reset ()
101 Errors = Warnings = 0;
102 WarningsAreErrors = false;
103 warning_ignore_table = null;
104 warning_regions_table = null;
107 abstract class AbstractMessage {
109 static void Check (int code)
111 if (code == expected_error) {
112 Environment.Exit (0);
116 public abstract bool IsWarning { get; }
118 public abstract string MessageType { get; }
120 public virtual void Print (int code, string location, string text)
125 StringBuilder msg = new StringBuilder ();
126 if (location.Length != 0) {
127 msg.Append (location);
130 msg.AppendFormat ("{0} CS{1:0000}: {2}", MessageType, code, text);
131 Stderr.WriteLine (msg.ToString ());
133 foreach (string s in extra_information)
134 Stderr.WriteLine (s + MessageType);
136 extra_information.Clear ();
139 Console.WriteLine (FriendlyStackTrace (new StackTrace (true)));
142 if (!IsWarning || WarningsAreErrors)
143 throw new Exception (text);
149 public virtual void Print (int code, Location location, string text)
151 Print (code, location.IsNull ? "" : location.ToString (), text);
155 sealed class WarningMessage : AbstractMessage {
156 Location loc = Location.Null;
159 public WarningMessage (int level)
164 public override bool IsWarning {
168 bool IsEnabled (int code)
170 if (RootContext.WarningLevel < Level)
173 if (warning_ignore_table != null) {
174 if (warning_ignore_table.Contains (code)) {
179 if (warning_regions_table == null || loc.Equals (Location.Null))
182 WarningRegions regions = (WarningRegions)warning_regions_table [loc.Name];
186 return regions.IsWarningEnabled (code, loc.Row);
189 public override void Print(int code, string location, string text)
191 if (!IsEnabled (code)) {
192 extra_information.Clear ();
196 if (WarningsAreErrors) {
197 new ErrorMessage ().Print (code, location, text);
202 base.Print (code, location, text);
205 public override void Print(int code, Location location, string text)
208 base.Print (code, location, text);
211 public override string MessageType {
218 sealed class ErrorMessage : AbstractMessage {
220 public override void Print(int code, string location, string text)
223 base.Print (code, location, text);
226 public override bool IsWarning {
227 get { return false; }
230 public override string MessageType {
238 public static void FeatureIsNotStandardized (Location loc, string feature)
240 Report.Error (1644, loc, "Feature `{0}' cannot be used because it is not part of the standardized ISO C# language specification", feature);
243 public static string FriendlyStackTrace (Exception e)
245 return FriendlyStackTrace (new StackTrace (e, true));
248 static string FriendlyStackTrace (StackTrace t)
250 StringBuilder sb = new StringBuilder ();
252 bool foundUserCode = false;
254 for (int i = 0; i < t.FrameCount; i++) {
255 StackFrame f = t.GetFrame (i);
256 MethodBase mb = f.GetMethod ();
258 if (!foundUserCode && mb.ReflectedType == typeof (Report))
261 foundUserCode = true;
265 if (f.GetFileLineNumber () > 0)
266 sb.AppendFormat ("(at {0}:{1}) ", f.GetFileName (), f.GetFileLineNumber ());
268 sb.AppendFormat ("{0}.{1} (", mb.ReflectedType.Name, mb.Name);
271 foreach (ParameterInfo pi in mb.GetParameters ()) {
276 sb.Append (TypeManager.CSharpName (pi.ParameterType));
281 return sb.ToString ();
284 public static void StackTrace ()
286 Console.WriteLine (FriendlyStackTrace (new StackTrace (true)));
289 public static bool IsValidWarning (int code)
291 return Array.BinarySearch (AllWarnings, code) >= 0;
294 static public void RuntimeMissingSupport (Location loc, string feature)
296 Report.Error (-88, loc, "Your .NET Runtime does not support `{0}'. Please use the latest Mono runtime instead.", feature);
300 /// In most error cases is very useful to have information about symbol that caused the error.
301 /// Call this method before you call Report.Error when it makes sense.
303 static public void SymbolRelatedToPreviousError (Location loc, string symbol)
305 SymbolRelatedToPreviousError (loc.ToString (), symbol);
308 static public void SymbolRelatedToPreviousError (MemberInfo mi)
310 Type dt = TypeManager.DropGenericTypeArguments (mi.DeclaringType);
311 TypeContainer temp_ds = TypeManager.LookupTypeContainer (dt);
312 if (temp_ds == null) {
313 SymbolRelatedToPreviousError (dt.Assembly.Location, TypeManager.GetFullNameSignature (mi));
315 MethodBase mb = mi as MethodBase;
317 mb = TypeManager.DropGenericMethodArguments (mb);
318 IMethodData md = TypeManager.GetMethod (mb);
319 SymbolRelatedToPreviousError (md.Location, md.GetSignatureForError ());
323 MemberCore mc = temp_ds.GetDefinition (mi.Name);
324 SymbolRelatedToPreviousError (mc);
328 static public void SymbolRelatedToPreviousError (MemberCore mc)
330 SymbolRelatedToPreviousError (mc.Location, mc.GetSignatureForError ());
333 static public void SymbolRelatedToPreviousError (Type type)
335 type = TypeManager.DropGenericTypeArguments (type);
337 if (TypeManager.IsGenericParameter (type)) {
338 TypeParameter tp = TypeManager.LookupTypeParameter (type);
340 SymbolRelatedToPreviousError (tp.Location, "");
345 if (type is TypeBuilder) {
346 DeclSpace temp_ds = TypeManager.LookupDeclSpace (type);
347 SymbolRelatedToPreviousError (temp_ds.Location, TypeManager.CSharpName (type));
348 } else if (type.HasElementType) {
349 SymbolRelatedToPreviousError (type.GetElementType ());
351 SymbolRelatedToPreviousError (type.Assembly.Location, TypeManager.CSharpName (type));
355 static void SymbolRelatedToPreviousError (string loc, string symbol)
357 extra_information.Add (String.Format ("{0}: `{1}', name of symbol related to previous ", loc, symbol));
360 public static void ExtraInformation (Location loc, string msg)
362 extra_information.Add (String.Format ("{0} {1}", loc, msg));
365 public static WarningRegions RegisterWarningRegion (Location location)
367 if (warning_regions_table == null)
368 warning_regions_table = new Hashtable ();
370 WarningRegions regions = (WarningRegions)warning_regions_table [location.Name];
371 if (regions == null) {
372 regions = new WarningRegions ();
373 warning_regions_table.Add (location.Name, regions);
378 static public void Warning (int code, int level, Location loc, string message)
380 WarningMessage w = new WarningMessage (level);
381 w.Print (code, loc, message);
384 static public void Warning (int code, int level, Location loc, string format, string arg)
386 WarningMessage w = new WarningMessage (level);
387 w.Print (code, loc, String.Format (format, arg));
390 static public void Warning (int code, int level, Location loc, string format, string arg1, string arg2)
392 WarningMessage w = new WarningMessage (level);
393 w.Print (code, loc, String.Format (format, arg1, arg2));
396 static public void Warning (int code, int level, Location loc, string format, params string[] args)
398 WarningMessage w = new WarningMessage (level);
399 w.Print (code, loc, String.Format (format, args));
402 static public void Warning (int code, int level, string message)
404 Warning (code, level, Location.Null, message);
407 static public void Warning (int code, int level, string format, string arg)
409 Warning (code, level, Location.Null, format, arg);
412 static public void Warning (int code, int level, string format, string arg1, string arg2)
414 Warning (code, level, Location.Null, format, arg1, arg2);
417 static public void Warning (int code, int level, string format, params string[] args)
419 Warning (code, level, Location.Null, String.Format (format, args));
422 static public void Error (int code, Location loc, string error)
424 new ErrorMessage ().Print (code, loc, error);
427 static public void Error (int code, Location loc, string format, string arg)
429 new ErrorMessage ().Print (code, loc, String.Format (format, arg));
432 static public void Error (int code, Location loc, string format, string arg1, string arg2)
434 new ErrorMessage ().Print (code, loc, String.Format (format, arg1, arg2));
437 static public void Error (int code, Location loc, string format, params string[] args)
439 Error (code, loc, String.Format (format, args));
442 static public void Error (int code, string error)
444 Error (code, Location.Null, error);
447 static public void Error (int code, string format, string arg)
449 Error (code, Location.Null, format, arg);
452 static public void Error (int code, string format, string arg1, string arg2)
454 Error (code, Location.Null, format, arg1, arg2);
457 static public void Error (int code, string format, params string[] args)
459 Error (code, Location.Null, String.Format (format, args));
462 static public void SetIgnoreWarning (int code)
464 if (warning_ignore_table == null)
465 warning_ignore_table = new Hashtable ();
467 warning_ignore_table [code] = true;
470 static public int ExpectedError {
472 expected_error = value;
475 return expected_error;
479 public static int DebugFlags = 0;
481 [Conditional ("MCS_DEBUG")]
482 static public void Debug (string message, params object[] args)
484 Debug (4, message, args);
487 [Conditional ("MCS_DEBUG")]
488 static public void Debug (int category, string message, params object[] args)
490 if ((category & DebugFlags) == 0)
493 StringBuilder sb = new StringBuilder (message);
495 if ((args != null) && (args.Length > 0)) {
499 foreach (object arg in args) {
506 else if (arg is ICollection)
507 sb.Append (PrintCollection ((ICollection) arg));
513 Console.WriteLine (sb.ToString ());
516 static public string PrintCollection (ICollection collection)
518 StringBuilder sb = new StringBuilder ();
520 sb.Append (collection.GetType ());
524 foreach (object o in collection) {
533 return sb.ToString ();
537 public enum TimerType {
547 public enum CounterType {
556 static DateTime[] timer_start;
557 static TimeSpan[] timers;
558 static long[] timer_counters;
559 static long[] counters;
563 timer_start = new DateTime [(int) TimerType.CountTimers];
564 timers = new TimeSpan [(int) TimerType.CountTimers];
565 timer_counters = new long [(int) TimerType.CountTimers];
566 counters = new long [(int) CounterType.CountCounters];
568 for (int i = 0; i < (int) TimerType.CountTimers; i++) {
569 timer_start [i] = DateTime.Now;
570 timers [i] = TimeSpan.Zero;
574 [Conditional("TIMER")]
575 static public void IncrementCounter (CounterType which)
577 ++counters [(int) which];
580 [Conditional("TIMER")]
581 static public void StartTimer (TimerType which)
583 timer_start [(int) which] = DateTime.Now;
586 [Conditional("TIMER")]
587 static public void StopTimer (TimerType which)
589 timers [(int) which] += DateTime.Now - timer_start [(int) which];
590 ++timer_counters [(int) which];
593 [Conditional("TIMER")]
594 static public void ShowTimers ()
596 ShowTimer (TimerType.FindMembers, "- FindMembers timer");
597 ShowTimer (TimerType.TcFindMembers, "- TypeContainer.FindMembers timer");
598 ShowTimer (TimerType.MemberLookup, "- MemberLookup timer");
599 ShowTimer (TimerType.CachedLookup, "- CachedLookup timer");
600 ShowTimer (TimerType.CacheInit, "- Cache init");
601 ShowTimer (TimerType.MiscTimer, "- Misc timer");
603 ShowCounter (CounterType.FindMembers, "- Find members");
604 ShowCounter (CounterType.MemberCache, "- Member cache");
605 ShowCounter (CounterType.MiscCounter, "- Misc counter");
608 static public void ShowCounter (CounterType which, string msg)
610 Console.WriteLine ("{0} {1}", counters [(int) which], msg);
613 static public void ShowTimer (TimerType which, string msg)
616 "[{0:00}:{1:000}] {2} (used {3} times)",
617 (int) timers [(int) which].TotalSeconds,
618 timers [(int) which].Milliseconds, msg,
619 timer_counters [(int) which]);
623 public class InternalErrorException : Exception {
624 public InternalErrorException (Location loc, string text, Exception e)
625 : base (loc + " " + text, e)
629 public InternalErrorException ()
630 : base ("Internal error")
634 public InternalErrorException (string message)
641 /// Handles #pragma warning
643 public class WarningRegions {
645 abstract class PragmaCmd
649 protected PragmaCmd (int line)
654 public abstract bool IsEnabled (int code, bool previous);
657 class Disable : PragmaCmd
660 public Disable (int line, int code)
666 public override bool IsEnabled (int code, bool previous)
668 return this.code == code ? false : previous;
672 class DisableAll : PragmaCmd
674 public DisableAll (int line)
677 public override bool IsEnabled(int code, bool previous)
683 class Enable : PragmaCmd
686 public Enable (int line, int code)
692 public override bool IsEnabled(int code, bool previous)
694 return this.code == code ? true : previous;
698 class EnableAll : PragmaCmd
700 public EnableAll (int line)
703 public override bool IsEnabled(int code, bool previous)
710 ArrayList regions = new ArrayList ();
712 public void WarningDisable (int line)
714 regions.Add (new DisableAll (line));
717 public void WarningDisable (Location location, int code)
719 if (CheckWarningCode (code, location))
720 regions.Add (new Disable (location.Row, code));
723 public void WarningEnable (int line)
725 regions.Add (new EnableAll (line));
728 public void WarningEnable (Location location, int code)
730 if (CheckWarningCode (code, location))
731 regions.Add (new Enable (location.Row, code));
734 public bool IsWarningEnabled (int code, int src_line)
737 foreach (PragmaCmd pragma in regions) {
738 if (src_line < pragma.Line)
741 result = pragma.IsEnabled (code, result);
746 static bool CheckWarningCode (int code, Location loc)
748 if (Report.IsValidWarning (code))
751 Report.Warning (1691, 1, loc, "`{0}' is not a valid warning number", code.ToString ());