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, 278, 282,
79 419, 420, 429, 436, 440, 465, 467, 469,
80 612, 618, 626, 628, 642, 649, 652, 658, 659, 660, 661, 665, 672, 675,
83 1522, 1570, 1571, 1572, 1573, 1574, 1580, 1581, 1584, 1587, 1589, 1590, 1591, 1592,
84 1616, 1633, 1634, 1635, 1690, 1691, 1692,
88 3005, 3012, 3018, 3019, 3021, 3022, 3023, 3026, 3027,
90 402, 414, 693, 1058, 1700, 3024
96 // Just to be sure that binary search is working
97 Array.Sort (AllWarnings);
100 public static void Reset ()
102 Errors = Warnings = 0;
103 WarningsAreErrors = false;
104 warning_ignore_table = null;
105 warning_regions_table = null;
108 abstract class AbstractMessage {
110 static void Check (int code)
112 if (code == expected_error) {
113 Environment.Exit (0);
117 public abstract bool IsWarning { get; }
119 public abstract string MessageType { get; }
121 public virtual void Print (int code, string location, string text)
126 StringBuilder msg = new StringBuilder ();
127 if (location.Length != 0) {
128 msg.Append (location);
131 msg.AppendFormat ("{0} CS{1:0000}: {2}", MessageType, code, text);
132 Stderr.WriteLine (msg.ToString ());
134 foreach (string s in extra_information)
135 Stderr.WriteLine (s + MessageType + ")");
137 extra_information.Clear ();
140 Console.WriteLine (FriendlyStackTrace (new StackTrace (true)));
143 if (!IsWarning || WarningsAreErrors)
144 throw new Exception (text);
150 public virtual void Print (int code, Location location, string text)
152 Print (code, location.IsNull ? "" : location.ToString (), text);
156 sealed class WarningMessage : AbstractMessage {
157 Location loc = Location.Null;
160 public WarningMessage (int level)
165 public override bool IsWarning {
169 bool IsEnabled (int code)
171 if (RootContext.WarningLevel < Level)
174 if (warning_ignore_table != null) {
175 if (warning_ignore_table.Contains (code)) {
180 if (warning_regions_table == null || loc.Equals (Location.Null))
183 WarningRegions regions = (WarningRegions)warning_regions_table [loc.Name];
187 return regions.IsWarningEnabled (code, loc.Row);
190 public override void Print(int code, string location, string text)
192 if (!IsEnabled (code)) {
193 extra_information.Clear ();
197 if (WarningsAreErrors) {
198 new ErrorMessage ().Print (code, location, text);
203 base.Print (code, location, text);
206 public override void Print(int code, Location location, string text)
209 base.Print (code, location, text);
212 public override string MessageType {
219 sealed class ErrorMessage : AbstractMessage {
221 public override void Print(int code, string location, string text)
224 base.Print (code, location, text);
227 public override bool IsWarning {
228 get { return false; }
231 public override string MessageType {
239 public static void FeatureIsNotStandardized (Location loc, string feature)
241 Report.Error (1644, loc, "Feature `{0}' cannot be used because it is not part of the standardized ISO C# language specification", feature);
244 public static string FriendlyStackTrace (Exception e)
246 return FriendlyStackTrace (new StackTrace (e, true));
249 static string FriendlyStackTrace (StackTrace t)
251 StringBuilder sb = new StringBuilder ();
253 bool foundUserCode = false;
255 for (int i = 0; i < t.FrameCount; i++) {
256 StackFrame f = t.GetFrame (i);
257 MethodBase mb = f.GetMethod ();
259 if (!foundUserCode && mb.ReflectedType == typeof (Report))
262 foundUserCode = true;
266 if (f.GetFileLineNumber () > 0)
267 sb.AppendFormat ("(at {0}:{1}) ", f.GetFileName (), f.GetFileLineNumber ());
269 sb.AppendFormat ("{0}.{1} (", mb.ReflectedType.Name, mb.Name);
272 foreach (ParameterInfo pi in mb.GetParameters ()) {
277 sb.Append (TypeManager.CSharpName (pi.ParameterType));
282 return sb.ToString ();
285 public static void StackTrace ()
287 Console.WriteLine (FriendlyStackTrace (new StackTrace (true)));
290 public static bool IsValidWarning (int code)
292 return Array.BinarySearch (AllWarnings, code) >= 0;
295 static public void RuntimeMissingSupport (Location loc, string feature)
297 Report.Error (-88, loc, "Your .NET Runtime does not support `{0}'. Please use the latest Mono runtime instead.", feature);
301 /// In most error cases is very useful to have information about symbol that caused the error.
302 /// Call this method before you call Report.Error when it makes sense.
304 static public void SymbolRelatedToPreviousError (Location loc, string symbol)
306 SymbolRelatedToPreviousError (loc.ToString (), symbol);
309 static public void SymbolRelatedToPreviousError (MemberInfo mi)
311 Type dt = TypeManager.DropGenericTypeArguments (mi.DeclaringType);
312 DeclSpace temp_ds = TypeManager.LookupDeclSpace (dt);
313 if (temp_ds == null) {
314 SymbolRelatedToPreviousError (dt.Assembly.Location, TypeManager.GetFullNameSignature (mi));
316 MethodBase mb = mi as MethodBase;
318 mb = TypeManager.DropGenericMethodArguments (mb);
319 IMethodData md = TypeManager.GetMethod (mb);
320 SymbolRelatedToPreviousError (md.Location, md.GetSignatureForError ());
324 MemberCore mc = temp_ds.GetDefinition (mi.Name);
325 SymbolRelatedToPreviousError (mc);
329 static public void SymbolRelatedToPreviousError (MemberCore mc)
331 SymbolRelatedToPreviousError (mc.Location, mc.GetSignatureForError ());
334 static public void SymbolRelatedToPreviousError (Type type)
336 type = TypeManager.DropGenericTypeArguments (type);
338 if (TypeManager.IsGenericParameter (type)) {
339 TypeParameter tp = TypeManager.LookupTypeParameter (type);
341 SymbolRelatedToPreviousError (tp.Location, "");
346 if (type is TypeBuilder) {
347 DeclSpace temp_ds = TypeManager.LookupDeclSpace (type);
348 SymbolRelatedToPreviousError (temp_ds.Location, TypeManager.CSharpName (type));
349 } else if (type.HasElementType) {
350 SymbolRelatedToPreviousError (type.GetElementType ());
352 SymbolRelatedToPreviousError (type.Assembly.Location, TypeManager.CSharpName (type));
356 static void SymbolRelatedToPreviousError (string loc, string symbol)
358 extra_information.Add (String.Format ("{0} (Location of the symbol related to previous ", loc));
361 public static void ExtraInformation (Location loc, string msg)
363 extra_information.Add (String.Format ("{0} {1}", loc, msg));
366 public static WarningRegions RegisterWarningRegion (Location location)
368 if (warning_regions_table == null)
369 warning_regions_table = new Hashtable ();
371 WarningRegions regions = (WarningRegions)warning_regions_table [location.Name];
372 if (regions == null) {
373 regions = new WarningRegions ();
374 warning_regions_table.Add (location.Name, regions);
379 static public void Warning (int code, int level, Location loc, string message)
381 WarningMessage w = new WarningMessage (level);
382 w.Print (code, loc, message);
385 static public void Warning (int code, int level, Location loc, string format, string arg)
387 WarningMessage w = new WarningMessage (level);
388 w.Print (code, loc, String.Format (format, arg));
391 static public void Warning (int code, int level, Location loc, string format, string arg1, string arg2)
393 WarningMessage w = new WarningMessage (level);
394 w.Print (code, loc, String.Format (format, arg1, arg2));
397 static public void Warning (int code, int level, Location loc, string format, params string[] args)
399 WarningMessage w = new WarningMessage (level);
400 w.Print (code, loc, String.Format (format, args));
403 static public void Warning (int code, int level, string message)
405 Warning (code, level, Location.Null, message);
408 static public void Warning (int code, int level, string format, string arg)
410 Warning (code, level, Location.Null, format, arg);
413 static public void Warning (int code, int level, string format, string arg1, string arg2)
415 Warning (code, level, Location.Null, format, arg1, arg2);
418 static public void Warning (int code, int level, string format, params string[] args)
420 Warning (code, level, Location.Null, String.Format (format, args));
423 static public void Error (int code, Location loc, string error)
425 new ErrorMessage ().Print (code, loc, error);
428 static public void Error (int code, Location loc, string format, string arg)
430 new ErrorMessage ().Print (code, loc, String.Format (format, arg));
433 static public void Error (int code, Location loc, string format, string arg1, string arg2)
435 new ErrorMessage ().Print (code, loc, String.Format (format, arg1, arg2));
438 static public void Error (int code, Location loc, string format, params string[] args)
440 Error (code, loc, String.Format (format, args));
443 static public void Error (int code, string error)
445 Error (code, Location.Null, error);
448 static public void Error (int code, string format, string arg)
450 Error (code, Location.Null, format, arg);
453 static public void Error (int code, string format, string arg1, string arg2)
455 Error (code, Location.Null, format, arg1, arg2);
458 static public void Error (int code, string format, params string[] args)
460 Error (code, Location.Null, String.Format (format, args));
463 static public void SetIgnoreWarning (int code)
465 if (warning_ignore_table == null)
466 warning_ignore_table = new Hashtable ();
468 warning_ignore_table [code] = true;
471 static public int ExpectedError {
473 expected_error = value;
476 return expected_error;
480 public static int DebugFlags = 0;
482 [Conditional ("MCS_DEBUG")]
483 static public void Debug (string message, params object[] args)
485 Debug (4, message, args);
488 [Conditional ("MCS_DEBUG")]
489 static public void Debug (int category, string message, params object[] args)
491 if ((category & DebugFlags) == 0)
494 StringBuilder sb = new StringBuilder (message);
496 if ((args != null) && (args.Length > 0)) {
500 foreach (object arg in args) {
507 else if (arg is ICollection)
508 sb.Append (PrintCollection ((ICollection) arg));
514 Console.WriteLine (sb.ToString ());
517 static public string PrintCollection (ICollection collection)
519 StringBuilder sb = new StringBuilder ();
521 sb.Append (collection.GetType ());
525 foreach (object o in collection) {
534 return sb.ToString ();
538 public enum TimerType {
548 public enum CounterType {
557 static DateTime[] timer_start;
558 static TimeSpan[] timers;
559 static long[] timer_counters;
560 static long[] counters;
564 timer_start = new DateTime [(int) TimerType.CountTimers];
565 timers = new TimeSpan [(int) TimerType.CountTimers];
566 timer_counters = new long [(int) TimerType.CountTimers];
567 counters = new long [(int) CounterType.CountCounters];
569 for (int i = 0; i < (int) TimerType.CountTimers; i++) {
570 timer_start [i] = DateTime.Now;
571 timers [i] = TimeSpan.Zero;
575 [Conditional("TIMER")]
576 static public void IncrementCounter (CounterType which)
578 ++counters [(int) which];
581 [Conditional("TIMER")]
582 static public void StartTimer (TimerType which)
584 timer_start [(int) which] = DateTime.Now;
587 [Conditional("TIMER")]
588 static public void StopTimer (TimerType which)
590 timers [(int) which] += DateTime.Now - timer_start [(int) which];
591 ++timer_counters [(int) which];
594 [Conditional("TIMER")]
595 static public void ShowTimers ()
597 ShowTimer (TimerType.FindMembers, "- FindMembers timer");
598 ShowTimer (TimerType.TcFindMembers, "- TypeContainer.FindMembers timer");
599 ShowTimer (TimerType.MemberLookup, "- MemberLookup timer");
600 ShowTimer (TimerType.CachedLookup, "- CachedLookup timer");
601 ShowTimer (TimerType.CacheInit, "- Cache init");
602 ShowTimer (TimerType.MiscTimer, "- Misc timer");
604 ShowCounter (CounterType.FindMembers, "- Find members");
605 ShowCounter (CounterType.MemberCache, "- Member cache");
606 ShowCounter (CounterType.MiscCounter, "- Misc counter");
609 static public void ShowCounter (CounterType which, string msg)
611 Console.WriteLine ("{0} {1}", counters [(int) which], msg);
614 static public void ShowTimer (TimerType which, string msg)
617 "[{0:00}:{1:000}] {2} (used {3} times)",
618 (int) timers [(int) which].TotalSeconds,
619 timers [(int) which].Milliseconds, msg,
620 timer_counters [(int) which]);
624 public class InternalErrorException : Exception {
625 public InternalErrorException (Location loc, string text, Exception e)
626 : base (loc + " " + text, e)
630 public InternalErrorException ()
631 : base ("Internal error")
635 public InternalErrorException (string message)
640 public InternalErrorException (string message, params object[] args)
641 : base (String.Format (message, args))
646 /// Handles #pragma warning
648 public class WarningRegions {
650 abstract class PragmaCmd
654 protected PragmaCmd (int line)
659 public abstract bool IsEnabled (int code, bool previous);
662 class Disable : PragmaCmd
665 public Disable (int line, int code)
671 public override bool IsEnabled (int code, bool previous)
673 return this.code == code ? false : previous;
677 class DisableAll : PragmaCmd
679 public DisableAll (int line)
682 public override bool IsEnabled(int code, bool previous)
688 class Enable : PragmaCmd
691 public Enable (int line, int code)
697 public override bool IsEnabled(int code, bool previous)
699 return this.code == code ? true : previous;
703 class EnableAll : PragmaCmd
705 public EnableAll (int line)
708 public override bool IsEnabled(int code, bool previous)
715 ArrayList regions = new ArrayList ();
717 public void WarningDisable (int line)
719 regions.Add (new DisableAll (line));
722 public void WarningDisable (Location location, int code)
724 if (CheckWarningCode (code, location))
725 regions.Add (new Disable (location.Row, code));
728 public void WarningEnable (int line)
730 regions.Add (new EnableAll (line));
733 public void WarningEnable (Location location, int code)
735 if (CheckWarningCode (code, location))
736 regions.Add (new Enable (location.Row, code));
739 public bool IsWarningEnabled (int code, int src_line)
742 foreach (PragmaCmd pragma in regions) {
743 if (src_line < pragma.Line)
746 result = pragma.IsEnabled (code, result);
751 static bool CheckWarningCode (int code, Location loc)
753 if (Report.IsValidWarning (code))
756 Report.Warning (1691, 1, loc, "`{0}' is not a valid warning number", code.ToString ());