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
92 // Just to be sure that binary search is working
93 Array.Sort (AllWarnings);
96 public static void Reset ()
98 Errors = Warnings = 0;
99 WarningsAreErrors = false;
100 warning_ignore_table = null;
101 warning_regions_table = null;
104 abstract class AbstractMessage {
106 static void Check (int code)
108 if (code == expected_error) {
109 Environment.Exit (0);
113 public abstract bool IsWarning { get; }
115 public abstract string MessageType { get; }
117 public virtual void Print (int code, string location, string text)
122 StringBuilder msg = new StringBuilder ();
123 if (location.Length != 0) {
124 msg.Append (location);
127 msg.AppendFormat ("{0} CS{1:0000}: {2}", MessageType, code, text);
128 Stderr.WriteLine (msg.ToString ());
130 foreach (string s in extra_information)
131 Stderr.WriteLine (s + MessageType);
133 extra_information.Clear ();
136 Console.WriteLine (FriendlyStackTrace (new StackTrace (true)));
139 if (!IsWarning || WarningsAreErrors)
140 throw new Exception (text);
146 public virtual void Print (int code, Location location, string text)
148 if (location.IsNull) {
149 Print (code, "", text);
152 Print (code, 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 TypeContainer temp_ds = TypeManager.LookupTypeContainer (mi.DeclaringType);
312 if (temp_ds == null) {
313 SymbolRelatedToPreviousError (mi.DeclaringType.Assembly.Location, TypeManager.GetFullNameSignature (mi));
315 if (mi is MethodBase) {
316 IMethodData md = TypeManager.GetMethod ((MethodBase)mi);
317 SymbolRelatedToPreviousError (md.Location, md.GetSignatureForError ());
321 MemberCore mc = temp_ds.GetDefinition (mi.Name);
322 SymbolRelatedToPreviousError (mc);
326 static public void SymbolRelatedToPreviousError (MemberCore mc)
328 SymbolRelatedToPreviousError (mc.Location, mc.GetSignatureForError ());
331 static public void SymbolRelatedToPreviousError (Type type)
333 if (type is TypeBuilder) {
334 DeclSpace temp_ds = TypeManager.LookupDeclSpace (type);
335 SymbolRelatedToPreviousError (temp_ds.Location, TypeManager.CSharpName (type));
336 } else if (type.HasElementType) {
337 SymbolRelatedToPreviousError (type.GetElementType ());
339 SymbolRelatedToPreviousError (type.Assembly.Location, TypeManager.CSharpName (type));
343 static void SymbolRelatedToPreviousError (string loc, string symbol)
345 extra_information.Add (String.Format ("{0}: `{1}', name of symbol related to previous ", loc, symbol));
348 public static void ExtraInformation (Location loc, string msg)
350 extra_information.Add (String.Format ("{0} {1}", loc, msg));
353 public static WarningRegions RegisterWarningRegion (Location location)
355 if (warning_regions_table == null)
356 warning_regions_table = new Hashtable ();
358 WarningRegions regions = (WarningRegions)warning_regions_table [location.Name];
359 if (regions == null) {
360 regions = new WarningRegions ();
361 warning_regions_table.Add (location.Name, regions);
366 static public void Warning (int code, int level, Location loc, string format, params string[] args)
368 WarningMessage w = new WarningMessage (level);
369 w.Print (code, loc, String.Format (format, args));
372 static public void Warning (int code, int level, string format, params string[] args)
374 Warning (code, level, Location.Null, String.Format (format, args));
377 static public void Error (int code, string format, params string[] args)
379 Error (code, Location.Null, String.Format (format, args));
382 static public void Error (int code, Location loc, string format, params string[] args)
384 Error (code, loc, String.Format (format, args));
387 static void Error (int code, Location loc, string error)
389 new ErrorMessage ().Print (code, loc, error);
392 static public void SetIgnoreWarning (int code)
394 if (warning_ignore_table == null)
395 warning_ignore_table = new Hashtable ();
397 warning_ignore_table [code] = true;
400 static public int ExpectedError {
402 expected_error = value;
405 return expected_error;
409 public static int DebugFlags = 0;
411 [Conditional ("MCS_DEBUG")]
412 static public void Debug (string message, params object[] args)
414 Debug (4, message, args);
417 [Conditional ("MCS_DEBUG")]
418 static public void Debug (int category, string message, params object[] args)
420 if ((category & DebugFlags) == 0)
423 StringBuilder sb = new StringBuilder (message);
425 if ((args != null) && (args.Length > 0)) {
429 foreach (object arg in args) {
436 else if (arg is ICollection)
437 sb.Append (PrintCollection ((ICollection) arg));
443 Console.WriteLine (sb.ToString ());
446 static public string PrintCollection (ICollection collection)
448 StringBuilder sb = new StringBuilder ();
450 sb.Append (collection.GetType ());
454 foreach (object o in collection) {
463 return sb.ToString ();
467 public enum TimerType {
477 public enum CounterType {
486 static DateTime[] timer_start;
487 static TimeSpan[] timers;
488 static long[] timer_counters;
489 static long[] counters;
493 timer_start = new DateTime [(int) TimerType.CountTimers];
494 timers = new TimeSpan [(int) TimerType.CountTimers];
495 timer_counters = new long [(int) TimerType.CountTimers];
496 counters = new long [(int) CounterType.CountCounters];
498 for (int i = 0; i < (int) TimerType.CountTimers; i++) {
499 timer_start [i] = DateTime.Now;
500 timers [i] = TimeSpan.Zero;
504 [Conditional("TIMER")]
505 static public void IncrementCounter (CounterType which)
507 ++counters [(int) which];
510 [Conditional("TIMER")]
511 static public void StartTimer (TimerType which)
513 timer_start [(int) which] = DateTime.Now;
516 [Conditional("TIMER")]
517 static public void StopTimer (TimerType which)
519 timers [(int) which] += DateTime.Now - timer_start [(int) which];
520 ++timer_counters [(int) which];
523 [Conditional("TIMER")]
524 static public void ShowTimers ()
526 ShowTimer (TimerType.FindMembers, "- FindMembers timer");
527 ShowTimer (TimerType.TcFindMembers, "- TypeContainer.FindMembers timer");
528 ShowTimer (TimerType.MemberLookup, "- MemberLookup timer");
529 ShowTimer (TimerType.CachedLookup, "- CachedLookup timer");
530 ShowTimer (TimerType.CacheInit, "- Cache init");
531 ShowTimer (TimerType.MiscTimer, "- Misc timer");
533 ShowCounter (CounterType.FindMembers, "- Find members");
534 ShowCounter (CounterType.MemberCache, "- Member cache");
535 ShowCounter (CounterType.MiscCounter, "- Misc counter");
538 static public void ShowCounter (CounterType which, string msg)
540 Console.WriteLine ("{0} {1}", counters [(int) which], msg);
543 static public void ShowTimer (TimerType which, string msg)
546 "[{0:00}:{1:000}] {2} (used {3} times)",
547 (int) timers [(int) which].TotalSeconds,
548 timers [(int) which].Milliseconds, msg,
549 timer_counters [(int) which]);
553 public class InternalErrorException : Exception {
554 public InternalErrorException ()
555 : base ("Internal error")
559 public InternalErrorException (string message)
566 /// Handles #pragma warning
568 public class WarningRegions {
570 abstract class PragmaCmd
574 protected PragmaCmd (int line)
579 public abstract bool IsEnabled (int code, bool previous);
582 class Disable : PragmaCmd
585 public Disable (int line, int code)
591 public override bool IsEnabled (int code, bool previous)
593 return this.code == code ? false : previous;
597 class DisableAll : PragmaCmd
599 public DisableAll (int line)
602 public override bool IsEnabled(int code, bool previous)
608 class Enable : PragmaCmd
611 public Enable (int line, int code)
617 public override bool IsEnabled(int code, bool previous)
619 return this.code == code ? true : previous;
623 class EnableAll : PragmaCmd
625 public EnableAll (int line)
628 public override bool IsEnabled(int code, bool previous)
635 ArrayList regions = new ArrayList ();
637 public void WarningDisable (int line)
639 regions.Add (new DisableAll (line));
642 public void WarningDisable (Location location, int code)
644 if (CheckWarningCode (code, location))
645 regions.Add (new Disable (location.Row, code));
648 public void WarningEnable (int line)
650 regions.Add (new EnableAll (line));
653 public void WarningEnable (Location location, int code)
655 if (CheckWarningCode (code, location))
656 regions.Add (new Enable (location.Row, code));
659 public bool IsWarningEnabled (int code, int src_line)
662 foreach (PragmaCmd pragma in regions) {
663 if (src_line < pragma.Line)
666 result = pragma.IsEnabled (code, result);
671 bool CheckWarningCode (int code, Location loc)
673 if (Report.IsValidWarning (code))
676 Report.Warning (1691, 1, loc, "`{0}' is not a valid warning number", code.ToString ());