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;
47 /// Whether to dump a stack trace on errors.
49 static public bool Stacktrace;
51 static public TextWriter Stderr = Console.Error;
54 // If the 'expected' error code is reported then the
55 // compilation succeeds.
57 // Used for the test suite to excercise the error codes
59 static int expected_error = 0;
62 // Keeps track of the warnings that we are ignoring
64 public static Hashtable warning_ignore_table;
66 static Hashtable warning_regions_table;
69 // This is used to save/restore the error state. When the
70 // error stack contains elements, warnings and errors are not
71 // reported to the user. This is used by the Lambda expression
72 // support to compile the code with various parameter values.
73 // A stack because of `Report.Errors == errors;'
75 static Stack error_stack;
76 static bool reporting_disabled;
78 static int warning_level;
81 /// List of symbols related to reported error/warning. You have to fill it before error/warning is reported.
83 static StringCollection extra_information = new StringCollection ();
86 // IF YOU ADD A NEW WARNING YOU HAVE TO ADD ITS ID HERE
88 public static readonly int[] AllWarnings = new int[] {
90 105, 108, 109, 114, 162, 164, 168, 169, 183, 184, 197,
91 219, 251, 252, 253, 278, 282,
92 419, 420, 429, 436, 440, 465, 467, 469,
93 612, 618, 626, 628, 642, 649, 652, 658, 659, 660, 661, 665, 672, 675,
96 1522, 1570, 1571, 1572, 1573, 1574, 1580, 1581, 1584, 1587, 1589, 1590, 1591, 1592,
97 1616, 1633, 1634, 1635, 1685, 1690, 1691, 1692,
101 3005, 3012, 3018, 3019, 3021, 3022, 3023, 3026, 3027,
103 402, 414, 693, 1058, 1700, 3024
109 // Just to be sure that binary search is working
110 Array.Sort (AllWarnings);
113 public static void Reset ()
115 Errors = Warnings = 0;
116 WarningsAreErrors = false;
117 warning_ignore_table = null;
118 warning_regions_table = null;
119 reporting_disabled = false;
122 public static void DisableReporting ()
124 if (error_stack == null)
125 error_stack = new Stack ();
126 error_stack.Push (Errors);
127 reporting_disabled = true;
130 public static void EnableReporting ()
132 Errors = (int) error_stack.Pop ();
133 if (error_stack.Count == 0) {
134 reporting_disabled = false;
138 abstract class AbstractMessage {
140 static void Check (int code)
142 if (code == expected_error) {
143 Environment.Exit (0);
147 public abstract bool IsWarning { get; }
149 public abstract string MessageType { get; }
151 public virtual void Print (int code, Location location, string text)
153 if (reporting_disabled) {
155 // This line is useful when debugging the inner working of
156 // Lambdas when various compilation attempts are done.
158 //Console.WriteLine ("DISABLED: {0} {1} {2}", code, location, text);
165 StringBuilder msg = new StringBuilder ();
166 if (!location.IsNull) {
167 msg.Append (location.ToString ());
170 msg.AppendFormat ("{0} CS{1:0000}: {2}", MessageType, code, text);
171 Stderr.WriteLine (msg.ToString ());
173 foreach (string s in extra_information)
174 Stderr.WriteLine (s + MessageType + ")");
176 extra_information.Clear ();
179 Console.WriteLine (FriendlyStackTrace (new StackTrace (true)));
182 if (!IsWarning || WarningsAreErrors)
183 throw new Exception (text);
190 sealed class WarningMessage : AbstractMessage {
193 public WarningMessage (int level)
198 public override bool IsWarning {
202 bool IsEnabled (int code, Location loc)
204 if (WarningLevel < Level)
207 if (warning_ignore_table != null) {
208 if (warning_ignore_table.Contains (code)) {
213 if (warning_regions_table == null || loc.IsNull)
216 WarningRegions regions = (WarningRegions)warning_regions_table [loc.Name];
220 return regions.IsWarningEnabled (code, loc.Row);
223 public override void Print (int code, Location location, string text)
225 if (!IsEnabled (code, location)) {
226 extra_information.Clear ();
230 if (WarningsAreErrors) {
231 new ErrorMessage ().Print (code, location, text);
236 base.Print (code, location, text);
239 public override string MessageType {
246 sealed class ErrorMessage : AbstractMessage {
248 public override void Print(int code, Location location, string text)
251 base.Print (code, location, text);
254 public override bool IsWarning {
255 get { return false; }
258 public override string MessageType {
266 public static void FeatureIsNotISO1 (Location loc, string feature)
268 Report.Error (1644, loc,
269 "Feature `{0}' cannot be used because it is not part of the C# 1.0 language specification",
273 public static void FeatureRequiresLINQ (Location loc, string feature)
275 Report.Error (1644, loc,
276 "Feature `{0}' can only be used if the language level is LINQ", feature);
279 public static string FriendlyStackTrace (Exception e)
281 return FriendlyStackTrace (new StackTrace (e, true));
284 static string FriendlyStackTrace (StackTrace t)
286 StringBuilder sb = new StringBuilder ();
288 bool foundUserCode = false;
290 for (int i = 0; i < t.FrameCount; i++) {
291 StackFrame f = t.GetFrame (i);
292 MethodBase mb = f.GetMethod ();
294 if (!foundUserCode && mb.ReflectedType == typeof (Report))
297 foundUserCode = true;
301 if (f.GetFileLineNumber () > 0)
302 sb.AppendFormat ("(at {0}:{1}) ", f.GetFileName (), f.GetFileLineNumber ());
304 sb.AppendFormat ("{0}.{1} (", mb.ReflectedType.Name, mb.Name);
307 foreach (ParameterInfo pi in mb.GetParameters ()) {
312 sb.Append (TypeManager.CSharpName (pi.ParameterType));
317 return sb.ToString ();
320 public static void StackTrace ()
322 Console.WriteLine (FriendlyStackTrace (new StackTrace (true)));
325 public static bool IsValidWarning (int code)
327 return Array.BinarySearch (AllWarnings, code) >= 0;
330 static public void RuntimeMissingSupport (Location loc, string feature)
332 Report.Error (-88, loc, "Your .NET Runtime does not support `{0}'. Please use the latest Mono runtime instead.", feature);
336 /// In most error cases is very useful to have information about symbol that caused the error.
337 /// Call this method before you call Report.Error when it makes sense.
339 static public void SymbolRelatedToPreviousError (Location loc, string symbol)
341 SymbolRelatedToPreviousError (loc.ToString (), symbol);
344 static public void SymbolRelatedToPreviousError (MemberInfo mi)
346 if (reporting_disabled)
349 Type dt = TypeManager.DropGenericTypeArguments (mi.DeclaringType);
350 DeclSpace temp_ds = TypeManager.LookupDeclSpace (dt);
351 if (temp_ds == null) {
352 SymbolRelatedToPreviousError (dt.Assembly.Location, TypeManager.GetFullNameSignature (mi));
354 MethodBase mb = mi as MethodBase;
356 mb = TypeManager.DropGenericMethodArguments (mb);
357 IMethodData md = TypeManager.GetMethod (mb);
358 SymbolRelatedToPreviousError (md.Location, md.GetSignatureForError ());
362 MemberCore mc = temp_ds.GetDefinition (mi.Name);
363 SymbolRelatedToPreviousError (mc);
367 static public void SymbolRelatedToPreviousError (MemberCore mc)
369 SymbolRelatedToPreviousError (mc.Location, mc.GetSignatureForError ());
372 static public void SymbolRelatedToPreviousError (Type type)
374 if (reporting_disabled)
377 type = TypeManager.DropGenericTypeArguments (type);
379 if (TypeManager.IsGenericParameter (type)) {
380 TypeParameter tp = TypeManager.LookupTypeParameter (type);
382 SymbolRelatedToPreviousError (tp.Location, "");
387 if (type is TypeBuilder) {
388 DeclSpace temp_ds = TypeManager.LookupDeclSpace (type);
389 SymbolRelatedToPreviousError (temp_ds.Location, TypeManager.CSharpName (type));
390 } else if (type.HasElementType) {
391 SymbolRelatedToPreviousError (type.GetElementType ());
393 SymbolRelatedToPreviousError (type.Assembly.Location, TypeManager.CSharpName (type));
397 static void SymbolRelatedToPreviousError (string loc, string symbol)
399 extra_information.Add (String.Format ("{0} (Location of the symbol related to previous ", loc));
402 public static void ExtraInformation (Location loc, string msg)
404 if (reporting_disabled)
407 extra_information.Add (String.Format ("{0} {1}", loc, msg));
410 public static WarningRegions RegisterWarningRegion (Location location)
412 if (warning_regions_table == null)
413 warning_regions_table = new Hashtable ();
415 WarningRegions regions = (WarningRegions)warning_regions_table [location.Name];
416 if (regions == null) {
417 regions = new WarningRegions ();
418 warning_regions_table.Add (location.Name, regions);
423 static public void Warning (int code, int level, Location loc, string message)
425 WarningMessage w = new WarningMessage (level);
426 w.Print (code, loc, message);
429 static public void Warning (int code, int level, Location loc, string format, string arg)
431 WarningMessage w = new WarningMessage (level);
432 w.Print (code, loc, String.Format (format, arg));
435 static public void Warning (int code, int level, Location loc, string format, string arg1, string arg2)
437 WarningMessage w = new WarningMessage (level);
438 w.Print (code, loc, String.Format (format, arg1, arg2));
441 static public void Warning (int code, int level, Location loc, string format, params string[] args)
443 WarningMessage w = new WarningMessage (level);
444 w.Print (code, loc, String.Format (format, args));
447 static public void Warning (int code, int level, string message)
449 Warning (code, level, Location.Null, message);
452 static public void Warning (int code, int level, string format, string arg)
454 Warning (code, level, Location.Null, format, arg);
457 static public void Warning (int code, int level, string format, string arg1, string arg2)
459 Warning (code, level, Location.Null, format, arg1, arg2);
462 static public void Warning (int code, int level, string format, params string[] args)
464 Warning (code, level, Location.Null, String.Format (format, args));
467 static public void Error (int code, Location loc, string error)
469 new ErrorMessage ().Print (code, loc, error);
472 static public void Error (int code, Location loc, string format, string arg)
474 new ErrorMessage ().Print (code, loc, String.Format (format, arg));
477 static public void Error (int code, Location loc, string format, string arg1, string arg2)
479 new ErrorMessage ().Print (code, loc, String.Format (format, arg1, arg2));
482 static public void Error (int code, Location loc, string format, params string[] args)
484 Error (code, loc, String.Format (format, args));
487 static public void Error (int code, string error)
489 Error (code, Location.Null, error);
492 static public void Error (int code, string format, string arg)
494 Error (code, Location.Null, format, arg);
497 static public void Error (int code, string format, string arg1, string arg2)
499 Error (code, Location.Null, format, arg1, arg2);
502 static public void Error (int code, string format, params string[] args)
504 Error (code, Location.Null, String.Format (format, args));
507 static public void SetIgnoreWarning (int code)
509 if (warning_ignore_table == null)
510 warning_ignore_table = new Hashtable ();
512 warning_ignore_table [code] = true;
515 static public int ExpectedError {
517 expected_error = value;
520 return expected_error;
524 public static int WarningLevel {
526 if (reporting_disabled)
529 return warning_level;
532 warning_level = value;
536 public static int DebugFlags = 0;
538 [Conditional ("MCS_DEBUG")]
539 static public void Debug (string message, params object[] args)
541 Debug (4, message, args);
544 [Conditional ("MCS_DEBUG")]
545 static public void Debug (int category, string message, params object[] args)
547 if ((category & DebugFlags) == 0)
550 StringBuilder sb = new StringBuilder (message);
552 if ((args != null) && (args.Length > 0)) {
556 foreach (object arg in args) {
563 else if (arg is ICollection)
564 sb.Append (PrintCollection ((ICollection) arg));
570 Console.WriteLine (sb.ToString ());
573 static public string PrintCollection (ICollection collection)
575 StringBuilder sb = new StringBuilder ();
577 sb.Append (collection.GetType ());
581 foreach (object o in collection) {
590 return sb.ToString ();
594 public enum TimerType {
604 public enum CounterType {
613 static DateTime[] timer_start;
614 static TimeSpan[] timers;
615 static long[] timer_counters;
616 static long[] counters;
620 timer_start = new DateTime [(int) TimerType.CountTimers];
621 timers = new TimeSpan [(int) TimerType.CountTimers];
622 timer_counters = new long [(int) TimerType.CountTimers];
623 counters = new long [(int) CounterType.CountCounters];
625 for (int i = 0; i < (int) TimerType.CountTimers; i++) {
626 timer_start [i] = DateTime.Now;
627 timers [i] = TimeSpan.Zero;
631 [Conditional("TIMER")]
632 static public void IncrementCounter (CounterType which)
634 ++counters [(int) which];
637 [Conditional("TIMER")]
638 static public void StartTimer (TimerType which)
640 timer_start [(int) which] = DateTime.Now;
643 [Conditional("TIMER")]
644 static public void StopTimer (TimerType which)
646 timers [(int) which] += DateTime.Now - timer_start [(int) which];
647 ++timer_counters [(int) which];
650 [Conditional("TIMER")]
651 static public void ShowTimers ()
653 ShowTimer (TimerType.FindMembers, "- FindMembers timer");
654 ShowTimer (TimerType.TcFindMembers, "- TypeContainer.FindMembers timer");
655 ShowTimer (TimerType.MemberLookup, "- MemberLookup timer");
656 ShowTimer (TimerType.CachedLookup, "- CachedLookup timer");
657 ShowTimer (TimerType.CacheInit, "- Cache init");
658 ShowTimer (TimerType.MiscTimer, "- Misc timer");
660 ShowCounter (CounterType.FindMembers, "- Find members");
661 ShowCounter (CounterType.MemberCache, "- Member cache");
662 ShowCounter (CounterType.MiscCounter, "- Misc counter");
665 static public void ShowCounter (CounterType which, string msg)
667 Console.WriteLine ("{0} {1}", counters [(int) which], msg);
670 static public void ShowTimer (TimerType which, string msg)
673 "[{0:00}:{1:000}] {2} (used {3} times)",
674 (int) timers [(int) which].TotalSeconds,
675 timers [(int) which].Milliseconds, msg,
676 timer_counters [(int) which]);
680 public class InternalErrorException : Exception {
681 public InternalErrorException (MemberCore mc, Exception e)
682 : base (mc.Location + " " + mc.GetSignatureForError (), e)
686 public InternalErrorException ()
687 : base ("Internal error")
691 public InternalErrorException (string message)
696 public InternalErrorException (string message, params object[] args)
697 : base (String.Format (message, args))
702 /// Handles #pragma warning
704 public class WarningRegions {
706 abstract class PragmaCmd
710 protected PragmaCmd (int line)
715 public abstract bool IsEnabled (int code, bool previous);
718 class Disable : PragmaCmd
721 public Disable (int line, int code)
727 public override bool IsEnabled (int code, bool previous)
729 return this.code == code ? false : previous;
733 class DisableAll : PragmaCmd
735 public DisableAll (int line)
738 public override bool IsEnabled(int code, bool previous)
744 class Enable : PragmaCmd
747 public Enable (int line, int code)
753 public override bool IsEnabled(int code, bool previous)
755 return this.code == code ? true : previous;
759 class EnableAll : PragmaCmd
761 public EnableAll (int line)
764 public override bool IsEnabled(int code, bool previous)
771 ArrayList regions = new ArrayList ();
773 public void WarningDisable (int line)
775 regions.Add (new DisableAll (line));
778 public void WarningDisable (Location location, int code)
780 if (CheckWarningCode (code, location))
781 regions.Add (new Disable (location.Row, code));
784 public void WarningEnable (int line)
786 regions.Add (new EnableAll (line));
789 public void WarningEnable (Location location, int code)
791 if (CheckWarningCode (code, location))
792 regions.Add (new Enable (location.Row, code));
795 public bool IsWarningEnabled (int code, int src_line)
798 foreach (PragmaCmd pragma in regions) {
799 if (src_line < pragma.Line)
802 result = pragma.IsEnabled (code, result);
807 static bool CheckWarningCode (int code, Location loc)
809 if (Report.IsValidWarning (code))
812 Report.Warning (1691, 1, loc, "`{0}' is not a valid warning number", code.ToString ());