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){
145 // This line is useful when debugging the inner working of
146 // Lambdas when various compilation attempts are done.
148 //Console.WriteLine ("DISABLED: {0} {1} {2}", code, location, text);
155 StringBuilder msg = new StringBuilder ();
156 if (location.Length != 0) {
157 msg.Append (location);
160 msg.AppendFormat ("{0} CS{1:0000}: {2}", MessageType, code, text);
161 Stderr.WriteLine (msg.ToString ());
163 foreach (string s in extra_information)
164 Stderr.WriteLine (s + MessageType + ")");
166 extra_information.Clear ();
169 Console.WriteLine (FriendlyStackTrace (new StackTrace (true)));
172 if (!IsWarning || WarningsAreErrors)
173 throw new Exception (text);
179 public virtual void Print (int code, Location location, string text)
181 Print (code, location.IsNull ? "" : location.ToString (), text);
185 sealed class WarningMessage : AbstractMessage {
186 Location loc = Location.Null;
189 public WarningMessage (int level)
194 public override bool IsWarning {
198 bool IsEnabled (int code)
200 if (RootContext.WarningLevel < Level)
203 if (warning_ignore_table != null) {
204 if (warning_ignore_table.Contains (code)) {
209 if (warning_regions_table == null || loc.Equals (Location.Null))
212 WarningRegions regions = (WarningRegions)warning_regions_table [loc.Name];
216 return regions.IsWarningEnabled (code, loc.Row);
219 public override void Print(int code, string location, string text)
221 if (!IsEnabled (code)) {
222 extra_information.Clear ();
226 if (WarningsAreErrors) {
227 new ErrorMessage ().Print (code, location, text);
232 base.Print (code, location, text);
235 public override void Print(int code, Location location, string text)
238 base.Print (code, location, text);
241 public override string MessageType {
248 sealed class ErrorMessage : AbstractMessage {
250 public override void Print(int code, string location, string text)
253 base.Print (code, location, text);
256 public override bool IsWarning {
257 get { return false; }
260 public override string MessageType {
268 public static void FeatureIsNotISO1 (Location loc, string feature)
270 Report.Error (1644, loc,
271 "Feature `{0}' cannot be used because it is not part of the C# 1.0 language specification",
275 public static void FeatureRequiresLINQ (Location loc, string feature)
277 Report.Error (1644, loc,
278 "Feature `{0}' can only be used if the language level is LINQ", feature);
281 public static string FriendlyStackTrace (Exception e)
283 return FriendlyStackTrace (new StackTrace (e, true));
286 static string FriendlyStackTrace (StackTrace t)
288 StringBuilder sb = new StringBuilder ();
290 bool foundUserCode = false;
292 for (int i = 0; i < t.FrameCount; i++) {
293 StackFrame f = t.GetFrame (i);
294 MethodBase mb = f.GetMethod ();
296 if (!foundUserCode && mb.ReflectedType == typeof (Report))
299 foundUserCode = true;
303 if (f.GetFileLineNumber () > 0)
304 sb.AppendFormat ("(at {0}:{1}) ", f.GetFileName (), f.GetFileLineNumber ());
306 sb.AppendFormat ("{0}.{1} (", mb.ReflectedType.Name, mb.Name);
309 foreach (ParameterInfo pi in mb.GetParameters ()) {
314 sb.Append (TypeManager.CSharpName (pi.ParameterType));
319 return sb.ToString ();
322 public static void StackTrace ()
324 Console.WriteLine (FriendlyStackTrace (new StackTrace (true)));
327 public static bool IsValidWarning (int code)
329 return Array.BinarySearch (AllWarnings, code) >= 0;
332 static public void RuntimeMissingSupport (Location loc, string feature)
334 Report.Error (-88, loc, "Your .NET Runtime does not support `{0}'. Please use the latest Mono runtime instead.", feature);
338 /// In most error cases is very useful to have information about symbol that caused the error.
339 /// Call this method before you call Report.Error when it makes sense.
341 static public void SymbolRelatedToPreviousError (Location loc, string symbol)
343 SymbolRelatedToPreviousError (loc.ToString (), symbol);
346 static public void SymbolRelatedToPreviousError (MemberInfo mi)
348 Type dt = TypeManager.DropGenericTypeArguments (mi.DeclaringType);
349 DeclSpace temp_ds = TypeManager.LookupDeclSpace (dt);
350 if (temp_ds == null) {
351 SymbolRelatedToPreviousError (dt.Assembly.Location, TypeManager.GetFullNameSignature (mi));
353 MethodBase mb = mi as MethodBase;
355 mb = TypeManager.DropGenericMethodArguments (mb);
356 IMethodData md = TypeManager.GetMethod (mb);
357 SymbolRelatedToPreviousError (md.Location, md.GetSignatureForError ());
361 MemberCore mc = temp_ds.GetDefinition (mi.Name);
362 SymbolRelatedToPreviousError (mc);
366 static public void SymbolRelatedToPreviousError (MemberCore mc)
368 SymbolRelatedToPreviousError (mc.Location, mc.GetSignatureForError ());
371 static public void SymbolRelatedToPreviousError (Type type)
373 type = TypeManager.DropGenericTypeArguments (type);
375 if (TypeManager.IsGenericParameter (type)) {
376 TypeParameter tp = TypeManager.LookupTypeParameter (type);
378 SymbolRelatedToPreviousError (tp.Location, "");
383 if (type is TypeBuilder) {
384 DeclSpace temp_ds = TypeManager.LookupDeclSpace (type);
385 SymbolRelatedToPreviousError (temp_ds.Location, TypeManager.CSharpName (type));
386 } else if (type.HasElementType) {
387 SymbolRelatedToPreviousError (type.GetElementType ());
389 SymbolRelatedToPreviousError (type.Assembly.Location, TypeManager.CSharpName (type));
393 static void SymbolRelatedToPreviousError (string loc, string symbol)
395 extra_information.Add (String.Format ("{0} (Location of the symbol related to previous ", loc));
398 public static void ExtraInformation (Location loc, string msg)
400 extra_information.Add (String.Format ("{0} {1}", loc, msg));
403 public static WarningRegions RegisterWarningRegion (Location location)
405 if (warning_regions_table == null)
406 warning_regions_table = new Hashtable ();
408 WarningRegions regions = (WarningRegions)warning_regions_table [location.Name];
409 if (regions == null) {
410 regions = new WarningRegions ();
411 warning_regions_table.Add (location.Name, regions);
416 static public void Warning (int code, int level, Location loc, string message)
418 WarningMessage w = new WarningMessage (level);
419 w.Print (code, loc, message);
422 static public void Warning (int code, int level, Location loc, string format, string arg)
424 WarningMessage w = new WarningMessage (level);
425 w.Print (code, loc, String.Format (format, arg));
428 static public void Warning (int code, int level, Location loc, string format, string arg1, string arg2)
430 WarningMessage w = new WarningMessage (level);
431 w.Print (code, loc, String.Format (format, arg1, arg2));
434 static public void Warning (int code, int level, Location loc, string format, params string[] args)
436 WarningMessage w = new WarningMessage (level);
437 w.Print (code, loc, String.Format (format, args));
440 static public void Warning (int code, int level, string message)
442 Warning (code, level, Location.Null, message);
445 static public void Warning (int code, int level, string format, string arg)
447 Warning (code, level, Location.Null, format, arg);
450 static public void Warning (int code, int level, string format, string arg1, string arg2)
452 Warning (code, level, Location.Null, format, arg1, arg2);
455 static public void Warning (int code, int level, string format, params string[] args)
457 Warning (code, level, Location.Null, String.Format (format, args));
460 static public void Error (int code, Location loc, string error)
462 new ErrorMessage ().Print (code, loc, error);
465 static public void Error (int code, Location loc, string format, string arg)
467 new ErrorMessage ().Print (code, loc, String.Format (format, arg));
470 static public void Error (int code, Location loc, string format, string arg1, string arg2)
472 new ErrorMessage ().Print (code, loc, String.Format (format, arg1, arg2));
475 static public void Error (int code, Location loc, string format, params string[] args)
477 Error (code, loc, String.Format (format, args));
480 static public void Error (int code, string error)
482 Error (code, Location.Null, error);
485 static public void Error (int code, string format, string arg)
487 Error (code, Location.Null, format, arg);
490 static public void Error (int code, string format, string arg1, string arg2)
492 Error (code, Location.Null, format, arg1, arg2);
495 static public void Error (int code, string format, params string[] args)
497 Error (code, Location.Null, String.Format (format, args));
500 static public void SetIgnoreWarning (int code)
502 if (warning_ignore_table == null)
503 warning_ignore_table = new Hashtable ();
505 warning_ignore_table [code] = true;
508 static public int ExpectedError {
510 expected_error = value;
513 return expected_error;
517 public static int DebugFlags = 0;
519 [Conditional ("MCS_DEBUG")]
520 static public void Debug (string message, params object[] args)
522 Debug (4, message, args);
525 [Conditional ("MCS_DEBUG")]
526 static public void Debug (int category, string message, params object[] args)
528 if ((category & DebugFlags) == 0)
531 StringBuilder sb = new StringBuilder (message);
533 if ((args != null) && (args.Length > 0)) {
537 foreach (object arg in args) {
544 else if (arg is ICollection)
545 sb.Append (PrintCollection ((ICollection) arg));
551 Console.WriteLine (sb.ToString ());
554 static public string PrintCollection (ICollection collection)
556 StringBuilder sb = new StringBuilder ();
558 sb.Append (collection.GetType ());
562 foreach (object o in collection) {
571 return sb.ToString ();
575 public enum TimerType {
585 public enum CounterType {
594 static DateTime[] timer_start;
595 static TimeSpan[] timers;
596 static long[] timer_counters;
597 static long[] counters;
601 timer_start = new DateTime [(int) TimerType.CountTimers];
602 timers = new TimeSpan [(int) TimerType.CountTimers];
603 timer_counters = new long [(int) TimerType.CountTimers];
604 counters = new long [(int) CounterType.CountCounters];
606 for (int i = 0; i < (int) TimerType.CountTimers; i++) {
607 timer_start [i] = DateTime.Now;
608 timers [i] = TimeSpan.Zero;
612 [Conditional("TIMER")]
613 static public void IncrementCounter (CounterType which)
615 ++counters [(int) which];
618 [Conditional("TIMER")]
619 static public void StartTimer (TimerType which)
621 timer_start [(int) which] = DateTime.Now;
624 [Conditional("TIMER")]
625 static public void StopTimer (TimerType which)
627 timers [(int) which] += DateTime.Now - timer_start [(int) which];
628 ++timer_counters [(int) which];
631 [Conditional("TIMER")]
632 static public void ShowTimers ()
634 ShowTimer (TimerType.FindMembers, "- FindMembers timer");
635 ShowTimer (TimerType.TcFindMembers, "- TypeContainer.FindMembers timer");
636 ShowTimer (TimerType.MemberLookup, "- MemberLookup timer");
637 ShowTimer (TimerType.CachedLookup, "- CachedLookup timer");
638 ShowTimer (TimerType.CacheInit, "- Cache init");
639 ShowTimer (TimerType.MiscTimer, "- Misc timer");
641 ShowCounter (CounterType.FindMembers, "- Find members");
642 ShowCounter (CounterType.MemberCache, "- Member cache");
643 ShowCounter (CounterType.MiscCounter, "- Misc counter");
646 static public void ShowCounter (CounterType which, string msg)
648 Console.WriteLine ("{0} {1}", counters [(int) which], msg);
651 static public void ShowTimer (TimerType which, string msg)
654 "[{0:00}:{1:000}] {2} (used {3} times)",
655 (int) timers [(int) which].TotalSeconds,
656 timers [(int) which].Milliseconds, msg,
657 timer_counters [(int) which]);
661 public class InternalErrorException : Exception {
662 public InternalErrorException (MemberCore mc, Exception e)
663 : base (mc.Location + " " + mc.GetSignatureForError (), e)
667 public InternalErrorException ()
668 : base ("Internal error")
672 public InternalErrorException (string message)
677 public InternalErrorException (string message, params object[] args)
678 : base (String.Format (message, args))
683 /// Handles #pragma warning
685 public class WarningRegions {
687 abstract class PragmaCmd
691 protected PragmaCmd (int line)
696 public abstract bool IsEnabled (int code, bool previous);
699 class Disable : PragmaCmd
702 public Disable (int line, int code)
708 public override bool IsEnabled (int code, bool previous)
710 return this.code == code ? false : previous;
714 class DisableAll : PragmaCmd
716 public DisableAll (int line)
719 public override bool IsEnabled(int code, bool previous)
725 class Enable : PragmaCmd
728 public Enable (int line, int code)
734 public override bool IsEnabled(int code, bool previous)
736 return this.code == code ? true : previous;
740 class EnableAll : PragmaCmd
742 public EnableAll (int line)
745 public override bool IsEnabled(int code, bool previous)
752 ArrayList regions = new ArrayList ();
754 public void WarningDisable (int line)
756 regions.Add (new DisableAll (line));
759 public void WarningDisable (Location location, int code)
761 if (CheckWarningCode (code, location))
762 regions.Add (new Disable (location.Row, code));
765 public void WarningEnable (int line)
767 regions.Add (new EnableAll (line));
770 public void WarningEnable (Location location, int code)
772 if (CheckWarningCode (code, location))
773 regions.Add (new Enable (location.Row, code));
776 public bool IsWarningEnabled (int code, int src_line)
779 foreach (PragmaCmd pragma in regions) {
780 if (src_line < pragma.Line)
783 result = pragma.IsEnabled (code, result);
788 static bool CheckWarningCode (int code, Location loc)
790 if (Report.IsValidWarning (code))
793 Report.Warning (1691, 1, loc, "`{0}' is not a valid warning number", code.ToString ());