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 if (location.IsNull) {
151 Print (code, "", text);
154 Print (code, location.ToString (), text);
158 sealed class WarningMessage : AbstractMessage {
159 Location loc = Location.Null;
162 public WarningMessage (int level)
167 public override bool IsWarning {
171 bool IsEnabled (int code)
173 if (RootContext.WarningLevel < Level)
176 if (warning_ignore_table != null) {
177 if (warning_ignore_table.Contains (code)) {
182 if (warning_regions_table == null || loc.Equals (Location.Null))
185 WarningRegions regions = (WarningRegions)warning_regions_table [loc.Name];
189 return regions.IsWarningEnabled (code, loc.Row);
192 public override void Print(int code, string location, string text)
194 if (!IsEnabled (code)) {
195 extra_information.Clear ();
199 if (WarningsAreErrors) {
200 new ErrorMessage ().Print (code, location, text);
205 base.Print (code, location, text);
208 public override void Print(int code, Location location, string text)
211 base.Print (code, location, text);
214 public override string MessageType {
221 sealed class ErrorMessage : AbstractMessage {
223 public override void Print(int code, string location, string text)
226 base.Print (code, location, text);
229 public override bool IsWarning {
230 get { return false; }
233 public override string MessageType {
241 public static void FeatureIsNotStandardized (Location loc, string feature)
243 Report.Error (1644, loc, "Feature `{0}' cannot be used because it is not part of the standardized ISO C# language specification", feature);
246 public static string FriendlyStackTrace (Exception e)
248 return FriendlyStackTrace (new StackTrace (e, true));
251 static string FriendlyStackTrace (StackTrace t)
253 StringBuilder sb = new StringBuilder ();
255 bool foundUserCode = false;
257 for (int i = 0; i < t.FrameCount; i++) {
258 StackFrame f = t.GetFrame (i);
259 MethodBase mb = f.GetMethod ();
261 if (!foundUserCode && mb.ReflectedType == typeof (Report))
264 foundUserCode = true;
268 if (f.GetFileLineNumber () > 0)
269 sb.AppendFormat ("(at {0}:{1}) ", f.GetFileName (), f.GetFileLineNumber ());
271 sb.AppendFormat ("{0}.{1} (", mb.ReflectedType.Name, mb.Name);
274 foreach (ParameterInfo pi in mb.GetParameters ()) {
279 sb.Append (TypeManager.CSharpName (pi.ParameterType));
284 return sb.ToString ();
287 public static void StackTrace ()
289 Console.WriteLine (FriendlyStackTrace (new StackTrace (true)));
292 public static bool IsValidWarning (int code)
294 return Array.BinarySearch (AllWarnings, code) >= 0;
297 static public void RuntimeMissingSupport (Location loc, string feature)
299 Report.Error (-88, loc, "Your .NET Runtime does not support `{0}'. Please use the latest Mono runtime instead.", feature);
303 /// In most error cases is very useful to have information about symbol that caused the error.
304 /// Call this method before you call Report.Error when it makes sense.
306 static public void SymbolRelatedToPreviousError (Location loc, string symbol)
308 SymbolRelatedToPreviousError (loc.ToString (), symbol);
311 static public void SymbolRelatedToPreviousError (MemberInfo mi)
313 TypeContainer temp_ds = TypeManager.LookupGenericTypeContainer (mi.DeclaringType);
314 if (temp_ds == null) {
315 SymbolRelatedToPreviousError (mi.DeclaringType.Assembly.Location, TypeManager.GetFullNameSignature (mi));
317 MethodBase mb = mi as MethodBase;
319 mb = TypeManager.DropGenericMethodArguments (mb);
320 IMethodData md = TypeManager.GetMethod (mb);
321 SymbolRelatedToPreviousError (md.Location, md.GetSignatureForError ());
325 MemberCore mc = temp_ds.GetDefinition (mi.Name);
326 SymbolRelatedToPreviousError (mc);
330 static public void SymbolRelatedToPreviousError (MemberCore mc)
332 SymbolRelatedToPreviousError (mc.Location, mc.GetSignatureForError ());
335 static public void SymbolRelatedToPreviousError (Type type)
337 type = TypeManager.DropGenericTypeArguments (type);
339 if (type.IsGenericParameter) {
340 TypeParameter tp = TypeManager.LookupTypeParameter (type);
342 SymbolRelatedToPreviousError (tp.Location, "");
347 if (type is TypeBuilder) {
348 DeclSpace temp_ds = TypeManager.LookupDeclSpace (type);
349 SymbolRelatedToPreviousError (temp_ds.Location, TypeManager.CSharpName (type));
350 } else if (type.HasElementType) {
351 SymbolRelatedToPreviousError (type.GetElementType ());
353 SymbolRelatedToPreviousError (type.Assembly.Location, TypeManager.CSharpName (type));
357 static void SymbolRelatedToPreviousError (string loc, string symbol)
359 extra_information.Add (String.Format ("{0}: `{1}', name of symbol related to previous ", loc, symbol));
362 public static void ExtraInformation (Location loc, string msg)
364 extra_information.Add (String.Format ("{0} {1}", loc, msg));
367 public static WarningRegions RegisterWarningRegion (Location location)
369 if (warning_regions_table == null)
370 warning_regions_table = new Hashtable ();
372 WarningRegions regions = (WarningRegions)warning_regions_table [location.Name];
373 if (regions == null) {
374 regions = new WarningRegions ();
375 warning_regions_table.Add (location.Name, regions);
380 static public void Warning (int code, int level, Location loc, string format, params string[] args)
382 WarningMessage w = new WarningMessage (level);
383 w.Print (code, loc, String.Format (format, args));
386 static public void Warning (int code, int level, string format, params string[] args)
388 Warning (code, level, Location.Null, String.Format (format, args));
391 static public void Error (int code, string format, params string[] args)
393 Error (code, Location.Null, String.Format (format, args));
396 static public void Error (int code, Location loc, string format, params string[] args)
398 Error (code, loc, String.Format (format, args));
401 static void Error (int code, Location loc, string error)
403 new ErrorMessage ().Print (code, loc, error);
406 static public void SetIgnoreWarning (int code)
408 if (warning_ignore_table == null)
409 warning_ignore_table = new Hashtable ();
411 warning_ignore_table [code] = true;
414 static public int ExpectedError {
416 expected_error = value;
419 return expected_error;
423 public static int DebugFlags = 0;
425 [Conditional ("MCS_DEBUG")]
426 static public void Debug (string message, params object[] args)
428 Debug (4, message, args);
431 [Conditional ("MCS_DEBUG")]
432 static public void Debug (int category, string message, params object[] args)
434 if ((category & DebugFlags) == 0)
437 StringBuilder sb = new StringBuilder (message);
439 if ((args != null) && (args.Length > 0)) {
443 foreach (object arg in args) {
450 else if (arg is ICollection)
451 sb.Append (PrintCollection ((ICollection) arg));
457 Console.WriteLine (sb.ToString ());
460 static public string PrintCollection (ICollection collection)
462 StringBuilder sb = new StringBuilder ();
464 sb.Append (collection.GetType ());
468 foreach (object o in collection) {
477 return sb.ToString ();
481 public enum TimerType {
491 public enum CounterType {
500 static DateTime[] timer_start;
501 static TimeSpan[] timers;
502 static long[] timer_counters;
503 static long[] counters;
507 timer_start = new DateTime [(int) TimerType.CountTimers];
508 timers = new TimeSpan [(int) TimerType.CountTimers];
509 timer_counters = new long [(int) TimerType.CountTimers];
510 counters = new long [(int) CounterType.CountCounters];
512 for (int i = 0; i < (int) TimerType.CountTimers; i++) {
513 timer_start [i] = DateTime.Now;
514 timers [i] = TimeSpan.Zero;
518 [Conditional("TIMER")]
519 static public void IncrementCounter (CounterType which)
521 ++counters [(int) which];
524 [Conditional("TIMER")]
525 static public void StartTimer (TimerType which)
527 timer_start [(int) which] = DateTime.Now;
530 [Conditional("TIMER")]
531 static public void StopTimer (TimerType which)
533 timers [(int) which] += DateTime.Now - timer_start [(int) which];
534 ++timer_counters [(int) which];
537 [Conditional("TIMER")]
538 static public void ShowTimers ()
540 ShowTimer (TimerType.FindMembers, "- FindMembers timer");
541 ShowTimer (TimerType.TcFindMembers, "- TypeContainer.FindMembers timer");
542 ShowTimer (TimerType.MemberLookup, "- MemberLookup timer");
543 ShowTimer (TimerType.CachedLookup, "- CachedLookup timer");
544 ShowTimer (TimerType.CacheInit, "- Cache init");
545 ShowTimer (TimerType.MiscTimer, "- Misc timer");
547 ShowCounter (CounterType.FindMembers, "- Find members");
548 ShowCounter (CounterType.MemberCache, "- Member cache");
549 ShowCounter (CounterType.MiscCounter, "- Misc counter");
552 static public void ShowCounter (CounterType which, string msg)
554 Console.WriteLine ("{0} {1}", counters [(int) which], msg);
557 static public void ShowTimer (TimerType which, string msg)
560 "[{0:00}:{1:000}] {2} (used {3} times)",
561 (int) timers [(int) which].TotalSeconds,
562 timers [(int) which].Milliseconds, msg,
563 timer_counters [(int) which]);
567 public class InternalErrorException : Exception {
568 public InternalErrorException ()
569 : base ("Internal error")
573 public InternalErrorException (string message)
580 /// Handles #pragma warning
582 public class WarningRegions {
584 abstract class PragmaCmd
588 protected PragmaCmd (int line)
593 public abstract bool IsEnabled (int code, bool previous);
596 class Disable : PragmaCmd
599 public Disable (int line, int code)
605 public override bool IsEnabled (int code, bool previous)
607 return this.code == code ? false : previous;
611 class DisableAll : PragmaCmd
613 public DisableAll (int line)
616 public override bool IsEnabled(int code, bool previous)
622 class Enable : PragmaCmd
625 public Enable (int line, int code)
631 public override bool IsEnabled(int code, bool previous)
633 return this.code == code ? true : previous;
637 class EnableAll : PragmaCmd
639 public EnableAll (int line)
642 public override bool IsEnabled(int code, bool previous)
649 ArrayList regions = new ArrayList ();
651 public void WarningDisable (int line)
653 regions.Add (new DisableAll (line));
656 public void WarningDisable (Location location, int code)
658 if (CheckWarningCode (code, location))
659 regions.Add (new Disable (location.Row, code));
662 public void WarningEnable (int line)
664 regions.Add (new EnableAll (line));
667 public void WarningEnable (Location location, int code)
669 if (CheckWarningCode (code, location))
670 regions.Add (new Enable (location.Row, code));
673 public bool IsWarningEnabled (int code, int src_line)
676 foreach (PragmaCmd pragma in regions) {
677 if (src_line < pragma.Line)
680 result = pragma.IsEnabled (code, result);
685 bool CheckWarningCode (int code, Location loc)
687 if (Report.IsValidWarning (code))
690 Report.Warning (1691, 1, loc, "`{0}' is not a valid warning number", code.ToString ());