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, 1685, 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 FeatureIsNotISO1 (Location loc, string feature)
241 Report.Error (1644, loc,
242 "Feature `{0}' cannot be used because it is not part of the C# 1.0 language specification",
246 public static void FeatureRequiresLINQ (Location loc, string feature)
248 Report.Error (1644, loc,
249 "Feature `{0}' can only be used if the language level is LINQ", feature);
252 public static string FriendlyStackTrace (Exception e)
254 return FriendlyStackTrace (new StackTrace (e, true));
257 static string FriendlyStackTrace (StackTrace t)
259 StringBuilder sb = new StringBuilder ();
261 bool foundUserCode = false;
263 for (int i = 0; i < t.FrameCount; i++) {
264 StackFrame f = t.GetFrame (i);
265 MethodBase mb = f.GetMethod ();
267 if (!foundUserCode && mb.ReflectedType == typeof (Report))
270 foundUserCode = true;
274 if (f.GetFileLineNumber () > 0)
275 sb.AppendFormat ("(at {0}:{1}) ", f.GetFileName (), f.GetFileLineNumber ());
277 sb.AppendFormat ("{0}.{1} (", mb.ReflectedType.Name, mb.Name);
280 foreach (ParameterInfo pi in mb.GetParameters ()) {
285 sb.Append (TypeManager.CSharpName (pi.ParameterType));
290 return sb.ToString ();
293 public static void StackTrace ()
295 Console.WriteLine (FriendlyStackTrace (new StackTrace (true)));
298 public static bool IsValidWarning (int code)
300 return Array.BinarySearch (AllWarnings, code) >= 0;
303 static public void RuntimeMissingSupport (Location loc, string feature)
305 Report.Error (-88, loc, "Your .NET Runtime does not support `{0}'. Please use the latest Mono runtime instead.", feature);
309 /// In most error cases is very useful to have information about symbol that caused the error.
310 /// Call this method before you call Report.Error when it makes sense.
312 static public void SymbolRelatedToPreviousError (Location loc, string symbol)
314 SymbolRelatedToPreviousError (loc.ToString (), symbol);
317 static public void SymbolRelatedToPreviousError (MemberInfo mi)
319 Type dt = TypeManager.DropGenericTypeArguments (mi.DeclaringType);
320 DeclSpace temp_ds = TypeManager.LookupDeclSpace (dt);
321 if (temp_ds == null) {
322 SymbolRelatedToPreviousError (dt.Assembly.Location, TypeManager.GetFullNameSignature (mi));
324 MethodBase mb = mi as MethodBase;
326 mb = TypeManager.DropGenericMethodArguments (mb);
327 IMethodData md = TypeManager.GetMethod (mb);
328 SymbolRelatedToPreviousError (md.Location, md.GetSignatureForError ());
332 MemberCore mc = temp_ds.GetDefinition (mi.Name);
333 SymbolRelatedToPreviousError (mc);
337 static public void SymbolRelatedToPreviousError (MemberCore mc)
339 SymbolRelatedToPreviousError (mc.Location, mc.GetSignatureForError ());
342 static public void SymbolRelatedToPreviousError (Type type)
344 type = TypeManager.DropGenericTypeArguments (type);
346 if (TypeManager.IsGenericParameter (type)) {
347 TypeParameter tp = TypeManager.LookupTypeParameter (type);
349 SymbolRelatedToPreviousError (tp.Location, "");
354 if (type is TypeBuilder) {
355 DeclSpace temp_ds = TypeManager.LookupDeclSpace (type);
356 SymbolRelatedToPreviousError (temp_ds.Location, TypeManager.CSharpName (type));
357 } else if (type.HasElementType) {
358 SymbolRelatedToPreviousError (type.GetElementType ());
360 SymbolRelatedToPreviousError (type.Assembly.Location, TypeManager.CSharpName (type));
364 static void SymbolRelatedToPreviousError (string loc, string symbol)
366 extra_information.Add (String.Format ("{0} (Location of the symbol related to previous ", loc));
369 public static void ExtraInformation (Location loc, string msg)
371 extra_information.Add (String.Format ("{0} {1}", loc, msg));
374 public static WarningRegions RegisterWarningRegion (Location location)
376 if (warning_regions_table == null)
377 warning_regions_table = new Hashtable ();
379 WarningRegions regions = (WarningRegions)warning_regions_table [location.Name];
380 if (regions == null) {
381 regions = new WarningRegions ();
382 warning_regions_table.Add (location.Name, regions);
387 static public void Warning (int code, int level, Location loc, string message)
389 WarningMessage w = new WarningMessage (level);
390 w.Print (code, loc, message);
393 static public void Warning (int code, int level, Location loc, string format, string arg)
395 WarningMessage w = new WarningMessage (level);
396 w.Print (code, loc, String.Format (format, arg));
399 static public void Warning (int code, int level, Location loc, string format, string arg1, string arg2)
401 WarningMessage w = new WarningMessage (level);
402 w.Print (code, loc, String.Format (format, arg1, arg2));
405 static public void Warning (int code, int level, Location loc, string format, params string[] args)
407 WarningMessage w = new WarningMessage (level);
408 w.Print (code, loc, String.Format (format, args));
411 static public void Warning (int code, int level, string message)
413 Warning (code, level, Location.Null, message);
416 static public void Warning (int code, int level, string format, string arg)
418 Warning (code, level, Location.Null, format, arg);
421 static public void Warning (int code, int level, string format, string arg1, string arg2)
423 Warning (code, level, Location.Null, format, arg1, arg2);
426 static public void Warning (int code, int level, string format, params string[] args)
428 Warning (code, level, Location.Null, String.Format (format, args));
431 static public void Error (int code, Location loc, string error)
433 new ErrorMessage ().Print (code, loc, error);
436 static public void Error (int code, Location loc, string format, string arg)
438 new ErrorMessage ().Print (code, loc, String.Format (format, arg));
441 static public void Error (int code, Location loc, string format, string arg1, string arg2)
443 new ErrorMessage ().Print (code, loc, String.Format (format, arg1, arg2));
446 static public void Error (int code, Location loc, string format, params string[] args)
448 Error (code, loc, String.Format (format, args));
451 static public void Error (int code, string error)
453 Error (code, Location.Null, error);
456 static public void Error (int code, string format, string arg)
458 Error (code, Location.Null, format, arg);
461 static public void Error (int code, string format, string arg1, string arg2)
463 Error (code, Location.Null, format, arg1, arg2);
466 static public void Error (int code, string format, params string[] args)
468 Error (code, Location.Null, String.Format (format, args));
471 static public void SetIgnoreWarning (int code)
473 if (warning_ignore_table == null)
474 warning_ignore_table = new Hashtable ();
476 warning_ignore_table [code] = true;
479 static public int ExpectedError {
481 expected_error = value;
484 return expected_error;
488 public static int DebugFlags = 0;
490 [Conditional ("MCS_DEBUG")]
491 static public void Debug (string message, params object[] args)
493 Debug (4, message, args);
496 [Conditional ("MCS_DEBUG")]
497 static public void Debug (int category, string message, params object[] args)
499 if ((category & DebugFlags) == 0)
502 StringBuilder sb = new StringBuilder (message);
504 if ((args != null) && (args.Length > 0)) {
508 foreach (object arg in args) {
515 else if (arg is ICollection)
516 sb.Append (PrintCollection ((ICollection) arg));
522 Console.WriteLine (sb.ToString ());
525 static public string PrintCollection (ICollection collection)
527 StringBuilder sb = new StringBuilder ();
529 sb.Append (collection.GetType ());
533 foreach (object o in collection) {
542 return sb.ToString ();
546 public enum TimerType {
556 public enum CounterType {
565 static DateTime[] timer_start;
566 static TimeSpan[] timers;
567 static long[] timer_counters;
568 static long[] counters;
572 timer_start = new DateTime [(int) TimerType.CountTimers];
573 timers = new TimeSpan [(int) TimerType.CountTimers];
574 timer_counters = new long [(int) TimerType.CountTimers];
575 counters = new long [(int) CounterType.CountCounters];
577 for (int i = 0; i < (int) TimerType.CountTimers; i++) {
578 timer_start [i] = DateTime.Now;
579 timers [i] = TimeSpan.Zero;
583 [Conditional("TIMER")]
584 static public void IncrementCounter (CounterType which)
586 ++counters [(int) which];
589 [Conditional("TIMER")]
590 static public void StartTimer (TimerType which)
592 timer_start [(int) which] = DateTime.Now;
595 [Conditional("TIMER")]
596 static public void StopTimer (TimerType which)
598 timers [(int) which] += DateTime.Now - timer_start [(int) which];
599 ++timer_counters [(int) which];
602 [Conditional("TIMER")]
603 static public void ShowTimers ()
605 ShowTimer (TimerType.FindMembers, "- FindMembers timer");
606 ShowTimer (TimerType.TcFindMembers, "- TypeContainer.FindMembers timer");
607 ShowTimer (TimerType.MemberLookup, "- MemberLookup timer");
608 ShowTimer (TimerType.CachedLookup, "- CachedLookup timer");
609 ShowTimer (TimerType.CacheInit, "- Cache init");
610 ShowTimer (TimerType.MiscTimer, "- Misc timer");
612 ShowCounter (CounterType.FindMembers, "- Find members");
613 ShowCounter (CounterType.MemberCache, "- Member cache");
614 ShowCounter (CounterType.MiscCounter, "- Misc counter");
617 static public void ShowCounter (CounterType which, string msg)
619 Console.WriteLine ("{0} {1}", counters [(int) which], msg);
622 static public void ShowTimer (TimerType which, string msg)
625 "[{0:00}:{1:000}] {2} (used {3} times)",
626 (int) timers [(int) which].TotalSeconds,
627 timers [(int) which].Milliseconds, msg,
628 timer_counters [(int) which]);
632 public class InternalErrorException : Exception {
633 public InternalErrorException (Location loc, string text, Exception e)
634 : base (loc + " " + text, e)
638 public InternalErrorException ()
639 : base ("Internal error")
643 public InternalErrorException (string message)
648 public InternalErrorException (string message, params object[] args)
649 : base (String.Format (message, args))
654 /// Handles #pragma warning
656 public class WarningRegions {
658 abstract class PragmaCmd
662 protected PragmaCmd (int line)
667 public abstract bool IsEnabled (int code, bool previous);
670 class Disable : PragmaCmd
673 public Disable (int line, int code)
679 public override bool IsEnabled (int code, bool previous)
681 return this.code == code ? false : previous;
685 class DisableAll : PragmaCmd
687 public DisableAll (int line)
690 public override bool IsEnabled(int code, bool previous)
696 class Enable : PragmaCmd
699 public Enable (int line, int code)
705 public override bool IsEnabled(int code, bool previous)
707 return this.code == code ? true : previous;
711 class EnableAll : PragmaCmd
713 public EnableAll (int line)
716 public override bool IsEnabled(int code, bool previous)
723 ArrayList regions = new ArrayList ();
725 public void WarningDisable (int line)
727 regions.Add (new DisableAll (line));
730 public void WarningDisable (Location location, int code)
732 if (CheckWarningCode (code, location))
733 regions.Add (new Disable (location.Row, code));
736 public void WarningEnable (int line)
738 regions.Add (new EnableAll (line));
741 public void WarningEnable (Location location, int code)
743 if (CheckWarningCode (code, location))
744 regions.Add (new Enable (location.Row, code));
747 public bool IsWarningEnabled (int code, int src_line)
750 foreach (PragmaCmd pragma in regions) {
751 if (src_line < pragma.Line)
754 result = pragma.IsEnabled (code, result);
759 static bool CheckWarningCode (int code, Location loc)
761 if (Report.IsValidWarning (code))
764 Report.Warning (1691, 1, loc, "`{0}' is not a valid warning number", code.ToString ());