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 // This is used to save/restore the error state. When the
69 // error stack contains elements, warnings and errors are not
70 // reported to the user. This is used by the Lambda expression
71 // support to compile the code with various parameter values.
73 static Stack error_stack;
76 /// List of symbols related to reported error/warning. You have to fill it before error/warning is reported.
78 static StringCollection extra_information = new StringCollection ();
81 // IF YOU ADD A NEW WARNING YOU HAVE TO ADD ITS ID HERE
83 public static readonly int[] AllWarnings = new int[] {
85 105, 108, 109, 114, 162, 164, 168, 169, 183, 184, 197,
86 219, 251, 252, 253, 278, 282,
87 419, 420, 429, 436, 440, 465, 467, 469,
88 612, 618, 626, 628, 642, 649, 652, 658, 659, 660, 661, 665, 672, 675,
91 1522, 1570, 1571, 1572, 1573, 1574, 1580, 1581, 1584, 1587, 1589, 1590, 1591, 1592,
92 1616, 1633, 1634, 1635, 1685, 1690, 1691, 1692,
96 3005, 3012, 3018, 3019, 3021, 3022, 3023, 3026, 3027,
98 402, 414, 693, 1058, 1700, 3024
104 // Just to be sure that binary search is working
105 Array.Sort (AllWarnings);
108 public static void Reset ()
110 Errors = Warnings = 0;
111 WarningsAreErrors = false;
112 warning_ignore_table = null;
113 warning_regions_table = null;
116 public static void DisableErrors ()
118 if (error_stack == null)
119 error_stack = new Stack ();
120 error_stack.Push (Errors);
123 public static void EnableErrors ()
125 Errors = (int) error_stack.Pop ();
128 abstract class AbstractMessage {
130 static void Check (int code)
132 if (code == expected_error) {
133 Environment.Exit (0);
137 public abstract bool IsWarning { get; }
139 public abstract string MessageType { get; }
141 public virtual void Print (int code, string location, string text)
143 if (error_stack != null && error_stack.Count != 0)
149 StringBuilder msg = new StringBuilder ();
150 if (location.Length != 0) {
151 msg.Append (location);
154 msg.AppendFormat ("{0} CS{1:0000}: {2}", MessageType, code, text);
155 Stderr.WriteLine (msg.ToString ());
157 foreach (string s in extra_information)
158 Stderr.WriteLine (s + MessageType + ")");
160 extra_information.Clear ();
163 Console.WriteLine (FriendlyStackTrace (new StackTrace (true)));
166 if (!IsWarning || WarningsAreErrors)
167 throw new Exception (text);
173 public virtual void Print (int code, Location location, string text)
175 Print (code, location.IsNull ? "" : location.ToString (), text);
179 sealed class WarningMessage : AbstractMessage {
180 Location loc = Location.Null;
183 public WarningMessage (int level)
188 public override bool IsWarning {
192 bool IsEnabled (int code)
194 if (RootContext.WarningLevel < Level)
197 if (warning_ignore_table != null) {
198 if (warning_ignore_table.Contains (code)) {
203 if (warning_regions_table == null || loc.Equals (Location.Null))
206 WarningRegions regions = (WarningRegions)warning_regions_table [loc.Name];
210 return regions.IsWarningEnabled (code, loc.Row);
213 public override void Print(int code, string location, string text)
215 if (!IsEnabled (code)) {
216 extra_information.Clear ();
220 if (WarningsAreErrors) {
221 new ErrorMessage ().Print (code, location, text);
226 base.Print (code, location, text);
229 public override void Print(int code, Location location, string text)
232 base.Print (code, location, text);
235 public override string MessageType {
242 sealed class ErrorMessage : AbstractMessage {
244 public override void Print(int code, string location, string text)
247 base.Print (code, location, text);
250 public override bool IsWarning {
251 get { return false; }
254 public override string MessageType {
262 public static void FeatureIsNotISO1 (Location loc, string feature)
264 Report.Error (1644, loc,
265 "Feature `{0}' cannot be used because it is not part of the C# 1.0 language specification",
269 public static void FeatureRequiresLINQ (Location loc, string feature)
271 Report.Error (1644, loc,
272 "Feature `{0}' can only be used if the language level is LINQ", feature);
275 public static string FriendlyStackTrace (Exception e)
277 return FriendlyStackTrace (new StackTrace (e, true));
280 static string FriendlyStackTrace (StackTrace t)
282 StringBuilder sb = new StringBuilder ();
284 bool foundUserCode = false;
286 for (int i = 0; i < t.FrameCount; i++) {
287 StackFrame f = t.GetFrame (i);
288 MethodBase mb = f.GetMethod ();
290 if (!foundUserCode && mb.ReflectedType == typeof (Report))
293 foundUserCode = true;
297 if (f.GetFileLineNumber () > 0)
298 sb.AppendFormat ("(at {0}:{1}) ", f.GetFileName (), f.GetFileLineNumber ());
300 sb.AppendFormat ("{0}.{1} (", mb.ReflectedType.Name, mb.Name);
303 foreach (ParameterInfo pi in mb.GetParameters ()) {
308 sb.Append (TypeManager.CSharpName (pi.ParameterType));
313 return sb.ToString ();
316 public static void StackTrace ()
318 Console.WriteLine (FriendlyStackTrace (new StackTrace (true)));
321 public static bool IsValidWarning (int code)
323 return Array.BinarySearch (AllWarnings, code) >= 0;
326 static public void RuntimeMissingSupport (Location loc, string feature)
328 Report.Error (-88, loc, "Your .NET Runtime does not support `{0}'. Please use the latest Mono runtime instead.", feature);
332 /// In most error cases is very useful to have information about symbol that caused the error.
333 /// Call this method before you call Report.Error when it makes sense.
335 static public void SymbolRelatedToPreviousError (Location loc, string symbol)
337 SymbolRelatedToPreviousError (loc.ToString (), symbol);
340 static public void SymbolRelatedToPreviousError (MemberInfo mi)
342 Type dt = TypeManager.DropGenericTypeArguments (mi.DeclaringType);
343 DeclSpace temp_ds = TypeManager.LookupDeclSpace (dt);
344 if (temp_ds == null) {
345 SymbolRelatedToPreviousError (dt.Assembly.Location, TypeManager.GetFullNameSignature (mi));
347 MethodBase mb = mi as MethodBase;
349 mb = TypeManager.DropGenericMethodArguments (mb);
350 IMethodData md = TypeManager.GetMethod (mb);
351 SymbolRelatedToPreviousError (md.Location, md.GetSignatureForError ());
355 MemberCore mc = temp_ds.GetDefinition (mi.Name);
356 SymbolRelatedToPreviousError (mc);
360 static public void SymbolRelatedToPreviousError (MemberCore mc)
362 SymbolRelatedToPreviousError (mc.Location, mc.GetSignatureForError ());
365 static public void SymbolRelatedToPreviousError (Type type)
367 type = TypeManager.DropGenericTypeArguments (type);
369 if (TypeManager.IsGenericParameter (type)) {
370 TypeParameter tp = TypeManager.LookupTypeParameter (type);
372 SymbolRelatedToPreviousError (tp.Location, "");
377 if (type is TypeBuilder) {
378 DeclSpace temp_ds = TypeManager.LookupDeclSpace (type);
379 SymbolRelatedToPreviousError (temp_ds.Location, TypeManager.CSharpName (type));
380 } else if (type.HasElementType) {
381 SymbolRelatedToPreviousError (type.GetElementType ());
383 SymbolRelatedToPreviousError (type.Assembly.Location, TypeManager.CSharpName (type));
387 static void SymbolRelatedToPreviousError (string loc, string symbol)
389 extra_information.Add (String.Format ("{0} (Location of the symbol related to previous ", loc));
392 public static void ExtraInformation (Location loc, string msg)
394 extra_information.Add (String.Format ("{0} {1}", loc, msg));
397 public static WarningRegions RegisterWarningRegion (Location location)
399 if (warning_regions_table == null)
400 warning_regions_table = new Hashtable ();
402 WarningRegions regions = (WarningRegions)warning_regions_table [location.Name];
403 if (regions == null) {
404 regions = new WarningRegions ();
405 warning_regions_table.Add (location.Name, regions);
410 static public void Warning (int code, int level, Location loc, string message)
412 WarningMessage w = new WarningMessage (level);
413 w.Print (code, loc, message);
416 static public void Warning (int code, int level, Location loc, string format, string arg)
418 WarningMessage w = new WarningMessage (level);
419 w.Print (code, loc, String.Format (format, arg));
422 static public void Warning (int code, int level, Location loc, string format, string arg1, string arg2)
424 WarningMessage w = new WarningMessage (level);
425 w.Print (code, loc, String.Format (format, arg1, arg2));
428 static public void Warning (int code, int level, Location loc, string format, params string[] args)
430 WarningMessage w = new WarningMessage (level);
431 w.Print (code, loc, String.Format (format, args));
434 static public void Warning (int code, int level, string message)
436 Warning (code, level, Location.Null, message);
439 static public void Warning (int code, int level, string format, string arg)
441 Warning (code, level, Location.Null, format, arg);
444 static public void Warning (int code, int level, string format, string arg1, string arg2)
446 Warning (code, level, Location.Null, format, arg1, arg2);
449 static public void Warning (int code, int level, string format, params string[] args)
451 Warning (code, level, Location.Null, String.Format (format, args));
454 static public void Error (int code, Location loc, string error)
456 new ErrorMessage ().Print (code, loc, error);
459 static public void Error (int code, Location loc, string format, string arg)
461 new ErrorMessage ().Print (code, loc, String.Format (format, arg));
464 static public void Error (int code, Location loc, string format, string arg1, string arg2)
466 new ErrorMessage ().Print (code, loc, String.Format (format, arg1, arg2));
469 static public void Error (int code, Location loc, string format, params string[] args)
471 Error (code, loc, String.Format (format, args));
474 static public void Error (int code, string error)
476 Error (code, Location.Null, error);
479 static public void Error (int code, string format, string arg)
481 Error (code, Location.Null, format, arg);
484 static public void Error (int code, string format, string arg1, string arg2)
486 Error (code, Location.Null, format, arg1, arg2);
489 static public void Error (int code, string format, params string[] args)
491 Error (code, Location.Null, String.Format (format, args));
494 static public void SetIgnoreWarning (int code)
496 if (warning_ignore_table == null)
497 warning_ignore_table = new Hashtable ();
499 warning_ignore_table [code] = true;
502 static public int ExpectedError {
504 expected_error = value;
507 return expected_error;
511 public static int DebugFlags = 0;
513 [Conditional ("MCS_DEBUG")]
514 static public void Debug (string message, params object[] args)
516 Debug (4, message, args);
519 [Conditional ("MCS_DEBUG")]
520 static public void Debug (int category, string message, params object[] args)
522 if ((category & DebugFlags) == 0)
525 StringBuilder sb = new StringBuilder (message);
527 if ((args != null) && (args.Length > 0)) {
531 foreach (object arg in args) {
538 else if (arg is ICollection)
539 sb.Append (PrintCollection ((ICollection) arg));
545 Console.WriteLine (sb.ToString ());
548 static public string PrintCollection (ICollection collection)
550 StringBuilder sb = new StringBuilder ();
552 sb.Append (collection.GetType ());
556 foreach (object o in collection) {
565 return sb.ToString ();
569 public enum TimerType {
579 public enum CounterType {
588 static DateTime[] timer_start;
589 static TimeSpan[] timers;
590 static long[] timer_counters;
591 static long[] counters;
595 timer_start = new DateTime [(int) TimerType.CountTimers];
596 timers = new TimeSpan [(int) TimerType.CountTimers];
597 timer_counters = new long [(int) TimerType.CountTimers];
598 counters = new long [(int) CounterType.CountCounters];
600 for (int i = 0; i < (int) TimerType.CountTimers; i++) {
601 timer_start [i] = DateTime.Now;
602 timers [i] = TimeSpan.Zero;
606 [Conditional("TIMER")]
607 static public void IncrementCounter (CounterType which)
609 ++counters [(int) which];
612 [Conditional("TIMER")]
613 static public void StartTimer (TimerType which)
615 timer_start [(int) which] = DateTime.Now;
618 [Conditional("TIMER")]
619 static public void StopTimer (TimerType which)
621 timers [(int) which] += DateTime.Now - timer_start [(int) which];
622 ++timer_counters [(int) which];
625 [Conditional("TIMER")]
626 static public void ShowTimers ()
628 ShowTimer (TimerType.FindMembers, "- FindMembers timer");
629 ShowTimer (TimerType.TcFindMembers, "- TypeContainer.FindMembers timer");
630 ShowTimer (TimerType.MemberLookup, "- MemberLookup timer");
631 ShowTimer (TimerType.CachedLookup, "- CachedLookup timer");
632 ShowTimer (TimerType.CacheInit, "- Cache init");
633 ShowTimer (TimerType.MiscTimer, "- Misc timer");
635 ShowCounter (CounterType.FindMembers, "- Find members");
636 ShowCounter (CounterType.MemberCache, "- Member cache");
637 ShowCounter (CounterType.MiscCounter, "- Misc counter");
640 static public void ShowCounter (CounterType which, string msg)
642 Console.WriteLine ("{0} {1}", counters [(int) which], msg);
645 static public void ShowTimer (TimerType which, string msg)
648 "[{0:00}:{1:000}] {2} (used {3} times)",
649 (int) timers [(int) which].TotalSeconds,
650 timers [(int) which].Milliseconds, msg,
651 timer_counters [(int) which]);
655 public class InternalErrorException : Exception {
656 public InternalErrorException (MemberCore mc, Exception e)
657 : base (mc.Location + " " + mc.GetSignatureForError (), e)
661 public InternalErrorException ()
662 : base ("Internal error")
666 public InternalErrorException (string message)
671 public InternalErrorException (string message, params object[] args)
672 : base (String.Format (message, args))
677 /// Handles #pragma warning
679 public class WarningRegions {
681 abstract class PragmaCmd
685 protected PragmaCmd (int line)
690 public abstract bool IsEnabled (int code, bool previous);
693 class Disable : PragmaCmd
696 public Disable (int line, int code)
702 public override bool IsEnabled (int code, bool previous)
704 return this.code == code ? false : previous;
708 class DisableAll : PragmaCmd
710 public DisableAll (int line)
713 public override bool IsEnabled(int code, bool previous)
719 class Enable : PragmaCmd
722 public Enable (int line, int code)
728 public override bool IsEnabled(int code, bool previous)
730 return this.code == code ? true : previous;
734 class EnableAll : PragmaCmd
736 public EnableAll (int line)
739 public override bool IsEnabled(int code, bool previous)
746 ArrayList regions = new ArrayList ();
748 public void WarningDisable (int line)
750 regions.Add (new DisableAll (line));
753 public void WarningDisable (Location location, int code)
755 if (CheckWarningCode (code, location))
756 regions.Add (new Disable (location.Row, code));
759 public void WarningEnable (int line)
761 regions.Add (new EnableAll (line));
764 public void WarningEnable (Location location, int code)
766 if (CheckWarningCode (code, location))
767 regions.Add (new Enable (location.Row, code));
770 public bool IsWarningEnabled (int code, int src_line)
773 foreach (PragmaCmd pragma in regions) {
774 if (src_line < pragma.Line)
777 result = pragma.IsEnabled (code, result);
782 static bool CheckWarningCode (int code, Location loc)
784 if (Report.IsValidWarning (code))
787 Report.Warning (1691, 1, loc, "`{0}' is not a valid warning number", code.ToString ());