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,
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, 1691, 1692,
87 3005, 3012, 3019, 3021, 3022, 3023, 3026, 3027,
88 // gmcs warnings start here
94 // Just to be sure that binary search is working
95 Array.Sort (AllWarnings);
98 public static void Reset ()
100 Errors = Warnings = 0;
101 WarningsAreErrors = false;
102 warning_ignore_table = null;
103 warning_regions_table = null;
106 abstract class AbstractMessage {
108 static void Check (int code)
110 if (code == expected_error) {
111 Environment.Exit (0);
115 public abstract bool IsWarning { get; }
117 public abstract string MessageType { get; }
119 public virtual void Print (int code, string location, string text)
124 StringBuilder msg = new StringBuilder ();
125 if (location.Length != 0) {
126 msg.Append (location);
129 msg.AppendFormat ("{0} CS{1:0000}: {2}", MessageType, code, text);
130 Stderr.WriteLine (msg.ToString ());
132 foreach (string s in extra_information)
133 Stderr.WriteLine (s + MessageType);
135 extra_information.Clear ();
138 Console.WriteLine (FriendlyStackTrace (new StackTrace (true)));
141 if (!IsWarning || WarningsAreErrors)
142 throw new Exception (text);
148 public virtual void Print (int code, Location location, string text)
150 Print (code, location.IsNull ? "" : location.ToString (), text);
154 sealed class WarningMessage : AbstractMessage {
155 Location loc = Location.Null;
158 public WarningMessage (int level)
163 public override bool IsWarning {
167 bool IsEnabled (int code)
169 if (RootContext.WarningLevel < Level)
172 if (warning_ignore_table != null) {
173 if (warning_ignore_table.Contains (code)) {
178 if (warning_regions_table == null || loc.Equals (Location.Null))
181 WarningRegions regions = (WarningRegions)warning_regions_table [loc.Name];
185 return regions.IsWarningEnabled (code, loc.Row);
188 public override void Print(int code, string location, string text)
190 if (!IsEnabled (code)) {
191 extra_information.Clear ();
195 if (WarningsAreErrors) {
196 new ErrorMessage ().Print (code, location, text);
201 base.Print (code, location, text);
204 public override void Print(int code, Location location, string text)
207 base.Print (code, location, text);
210 public override string MessageType {
217 sealed class ErrorMessage : AbstractMessage {
219 public override void Print(int code, string location, string text)
222 base.Print (code, location, text);
225 public override bool IsWarning {
226 get { return false; }
229 public override string MessageType {
237 public static void FeatureIsNotStandardized (Location loc, string feature)
239 Report.Error (1644, loc, "Feature `{0}' cannot be used because it is not part of the standardized ISO C# language specification", feature);
242 public static string FriendlyStackTrace (Exception e)
244 return FriendlyStackTrace (new StackTrace (e, true));
247 static string FriendlyStackTrace (StackTrace t)
249 StringBuilder sb = new StringBuilder ();
251 bool foundUserCode = false;
253 for (int i = 0; i < t.FrameCount; i++) {
254 StackFrame f = t.GetFrame (i);
255 MethodBase mb = f.GetMethod ();
257 if (!foundUserCode && mb.ReflectedType == typeof (Report))
260 foundUserCode = true;
264 if (f.GetFileLineNumber () > 0)
265 sb.AppendFormat ("(at {0}:{1}) ", f.GetFileName (), f.GetFileLineNumber ());
267 sb.AppendFormat ("{0}.{1} (", mb.ReflectedType.Name, mb.Name);
270 foreach (ParameterInfo pi in mb.GetParameters ()) {
275 sb.Append (TypeManager.CSharpName (pi.ParameterType));
280 return sb.ToString ();
283 public static void StackTrace ()
285 Console.WriteLine (FriendlyStackTrace (new StackTrace (true)));
288 public static bool IsValidWarning (int code)
290 return Array.BinarySearch (AllWarnings, code) >= 0;
293 static public void RuntimeMissingSupport (Location loc, string feature)
295 Report.Error (-88, loc, "Your .NET Runtime does not support `{0}'. Please use the latest Mono runtime instead.", feature);
299 /// In most error cases is very useful to have information about symbol that caused the error.
300 /// Call this method before you call Report.Error when it makes sense.
302 static public void SymbolRelatedToPreviousError (Location loc, string symbol)
304 SymbolRelatedToPreviousError (loc.ToString (), symbol);
307 static public void SymbolRelatedToPreviousError (MemberInfo mi)
309 Type dt = TypeManager.DropGenericTypeArguments (mi.DeclaringType);
310 TypeContainer temp_ds = TypeManager.LookupGenericTypeContainer (dt);
311 if (temp_ds == null) {
312 SymbolRelatedToPreviousError (dt.Assembly.Location, TypeManager.GetFullNameSignature (mi));
314 MethodBase mb = mi as MethodBase;
316 mb = TypeManager.DropGenericMethodArguments (mb);
317 IMethodData md = TypeManager.GetMethod (mb);
318 SymbolRelatedToPreviousError (md.Location, md.GetSignatureForError ());
322 MemberCore mc = temp_ds.GetDefinition (mi.Name);
323 SymbolRelatedToPreviousError (mc);
327 static public void SymbolRelatedToPreviousError (MemberCore mc)
329 SymbolRelatedToPreviousError (mc.Location, mc.GetSignatureForError ());
332 static public void SymbolRelatedToPreviousError (Type type)
334 type = TypeManager.DropGenericTypeArguments (type);
336 if (type.IsGenericParameter) {
337 TypeParameter tp = TypeManager.LookupTypeParameter (type);
339 SymbolRelatedToPreviousError (tp.Location, "");
344 if (type is TypeBuilder) {
345 DeclSpace temp_ds = TypeManager.LookupDeclSpace (type);
346 SymbolRelatedToPreviousError (temp_ds.Location, TypeManager.CSharpName (type));
347 } else if (type.HasElementType) {
348 SymbolRelatedToPreviousError (type.GetElementType ());
350 SymbolRelatedToPreviousError (type.Assembly.Location, TypeManager.CSharpName (type));
354 static void SymbolRelatedToPreviousError (string loc, string symbol)
356 extra_information.Add (String.Format ("{0}: `{1}', name of symbol related to previous ", loc, symbol));
359 public static void ExtraInformation (Location loc, string msg)
361 extra_information.Add (String.Format ("{0} {1}", loc, msg));
364 public static WarningRegions RegisterWarningRegion (Location location)
366 if (warning_regions_table == null)
367 warning_regions_table = new Hashtable ();
369 WarningRegions regions = (WarningRegions)warning_regions_table [location.Name];
370 if (regions == null) {
371 regions = new WarningRegions ();
372 warning_regions_table.Add (location.Name, regions);
377 static public void Warning (int code, int level, Location loc, string message)
379 WarningMessage w = new WarningMessage (level);
380 w.Print (code, loc, message);
383 static public void Warning (int code, int level, Location loc, string format, string arg)
385 WarningMessage w = new WarningMessage (level);
386 w.Print (code, loc, String.Format (format, arg));
389 static public void Warning (int code, int level, Location loc, string format, string arg1, string arg2)
391 WarningMessage w = new WarningMessage (level);
392 w.Print (code, loc, String.Format (format, arg1, arg2));
395 static public void Warning (int code, int level, Location loc, string format, params string[] args)
397 WarningMessage w = new WarningMessage (level);
398 w.Print (code, loc, String.Format (format, args));
401 static public void Warning (int code, int level, string message)
403 Warning (code, level, Location.Null, message);
406 static public void Warning (int code, int level, string format, string arg)
408 Warning (code, level, Location.Null, format, arg);
411 static public void Warning (int code, int level, string format, string arg1, string arg2)
413 Warning (code, level, Location.Null, format, arg1, arg2);
416 static public void Warning (int code, int level, string format, params string[] args)
418 Warning (code, level, Location.Null, String.Format (format, args));
421 static public void Error (int code, Location loc, string error)
423 new ErrorMessage ().Print (code, loc, error);
426 static public void Error (int code, Location loc, string format, string arg)
428 new ErrorMessage ().Print (code, loc, String.Format (format, arg));
431 static public void Error (int code, Location loc, string format, string arg1, string arg2)
433 new ErrorMessage ().Print (code, loc, String.Format (format, arg1, arg2));
436 static public void Error (int code, Location loc, string format, params string[] args)
438 Error (code, loc, String.Format (format, args));
441 static public void Error (int code, string error)
443 Error (code, Location.Null, error);
446 static public void Error (int code, string format, string arg)
448 Error (code, Location.Null, format, arg);
451 static public void Error (int code, string format, string arg1, string arg2)
453 Error (code, Location.Null, format, arg1, arg2);
456 static public void Error (int code, string format, params string[] args)
458 Error (code, Location.Null, String.Format (format, args));
461 static public void SetIgnoreWarning (int code)
463 if (warning_ignore_table == null)
464 warning_ignore_table = new Hashtable ();
466 warning_ignore_table [code] = true;
469 static public int ExpectedError {
471 expected_error = value;
474 return expected_error;
478 public static int DebugFlags = 0;
480 [Conditional ("MCS_DEBUG")]
481 static public void Debug (string message, params object[] args)
483 Debug (4, message, args);
486 [Conditional ("MCS_DEBUG")]
487 static public void Debug (int category, string message, params object[] args)
489 if ((category & DebugFlags) == 0)
492 StringBuilder sb = new StringBuilder (message);
494 if ((args != null) && (args.Length > 0)) {
498 foreach (object arg in args) {
505 else if (arg is ICollection)
506 sb.Append (PrintCollection ((ICollection) arg));
512 Console.WriteLine (sb.ToString ());
515 static public string PrintCollection (ICollection collection)
517 StringBuilder sb = new StringBuilder ();
519 sb.Append (collection.GetType ());
523 foreach (object o in collection) {
532 return sb.ToString ();
536 public enum TimerType {
546 public enum CounterType {
555 static DateTime[] timer_start;
556 static TimeSpan[] timers;
557 static long[] timer_counters;
558 static long[] counters;
562 timer_start = new DateTime [(int) TimerType.CountTimers];
563 timers = new TimeSpan [(int) TimerType.CountTimers];
564 timer_counters = new long [(int) TimerType.CountTimers];
565 counters = new long [(int) CounterType.CountCounters];
567 for (int i = 0; i < (int) TimerType.CountTimers; i++) {
568 timer_start [i] = DateTime.Now;
569 timers [i] = TimeSpan.Zero;
573 [Conditional("TIMER")]
574 static public void IncrementCounter (CounterType which)
576 ++counters [(int) which];
579 [Conditional("TIMER")]
580 static public void StartTimer (TimerType which)
582 timer_start [(int) which] = DateTime.Now;
585 [Conditional("TIMER")]
586 static public void StopTimer (TimerType which)
588 timers [(int) which] += DateTime.Now - timer_start [(int) which];
589 ++timer_counters [(int) which];
592 [Conditional("TIMER")]
593 static public void ShowTimers ()
595 ShowTimer (TimerType.FindMembers, "- FindMembers timer");
596 ShowTimer (TimerType.TcFindMembers, "- TypeContainer.FindMembers timer");
597 ShowTimer (TimerType.MemberLookup, "- MemberLookup timer");
598 ShowTimer (TimerType.CachedLookup, "- CachedLookup timer");
599 ShowTimer (TimerType.CacheInit, "- Cache init");
600 ShowTimer (TimerType.MiscTimer, "- Misc timer");
602 ShowCounter (CounterType.FindMembers, "- Find members");
603 ShowCounter (CounterType.MemberCache, "- Member cache");
604 ShowCounter (CounterType.MiscCounter, "- Misc counter");
607 static public void ShowCounter (CounterType which, string msg)
609 Console.WriteLine ("{0} {1}", counters [(int) which], msg);
612 static public void ShowTimer (TimerType which, string msg)
615 "[{0:00}:{1:000}] {2} (used {3} times)",
616 (int) timers [(int) which].TotalSeconds,
617 timers [(int) which].Milliseconds, msg,
618 timer_counters [(int) which]);
622 public class InternalErrorException : Exception {
623 public InternalErrorException (Location loc, string text, Exception e)
624 : base (loc + " " + text, e)
628 public InternalErrorException ()
629 : base ("Internal error")
633 public InternalErrorException (string message)
640 /// Handles #pragma warning
642 public class WarningRegions {
644 abstract class PragmaCmd
648 protected PragmaCmd (int line)
653 public abstract bool IsEnabled (int code, bool previous);
656 class Disable : PragmaCmd
659 public Disable (int line, int code)
665 public override bool IsEnabled (int code, bool previous)
667 return this.code == code ? false : previous;
671 class DisableAll : PragmaCmd
673 public DisableAll (int line)
676 public override bool IsEnabled(int code, bool previous)
682 class Enable : PragmaCmd
685 public Enable (int line, int code)
691 public override bool IsEnabled(int code, bool previous)
693 return this.code == code ? true : previous;
697 class EnableAll : PragmaCmd
699 public EnableAll (int line)
702 public override bool IsEnabled(int code, bool previous)
709 ArrayList regions = new ArrayList ();
711 public void WarningDisable (int line)
713 regions.Add (new DisableAll (line));
716 public void WarningDisable (Location location, int code)
718 if (CheckWarningCode (code, location))
719 regions.Add (new Disable (location.Row, code));
722 public void WarningEnable (int line)
724 regions.Add (new EnableAll (line));
727 public void WarningEnable (Location location, int code)
729 if (CheckWarningCode (code, location))
730 regions.Add (new Enable (location.Row, code));
733 public bool IsWarningEnabled (int code, int src_line)
736 foreach (PragmaCmd pragma in regions) {
737 if (src_line < pragma.Line)
740 result = pragma.IsEnabled (code, result);
745 static bool CheckWarningCode (int code, Location loc)
747 if (Report.IsValidWarning (code))
750 Report.Warning (1691, 1, loc, "`{0}' is not a valid warning number", code.ToString ());