2 // report.cs: report errors and warnings.
4 // Author: Miguel de Icaza (miguel@ximian.com)
6 // (C) 2001 Ximian, Inc. (http://www.ximian.com)
10 // FIXME: currently our class library does not support custom number format strings
14 using System.Collections;
15 using System.Collections.Specialized;
16 using System.Diagnostics;
17 using System.Reflection;
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;
51 // If the 'expected' error code is reported then the
52 // compilation succeeds.
54 // Used for the test suite to excercise the error codes
56 static int expected_error = 0;
59 // Keeps track of the warnings that we are ignoring
61 static Hashtable warning_ignore_table;
63 static Hashtable warning_regions_table;
66 /// List of symbols related to reported error/warning. You have to fill it before error/warning is reported.
68 static StringCollection related_symbols = new StringCollection ();
70 abstract class AbstractMessage {
72 static void Check (int code)
74 if (code == expected_error) {
79 public abstract string MessageType { get; }
81 public virtual void Print (int code, string location, string text)
86 StringBuilder msg = new StringBuilder ();
87 if (location.Length != 0) {
88 msg.Append (location);
91 msg.AppendFormat ("{0} CS{1:0000}: {2}", MessageType, code, text);
92 Console.WriteLine (msg.ToString ());
94 foreach (string s in related_symbols) {
95 Console.WriteLine (String.Concat (s, MessageType, ')'));
97 related_symbols.Clear ();
100 Console.WriteLine (FriendlyStackTrace (new StackTrace (true)));
103 throw new Exception (text);
108 public virtual void Print (int code, Location location, string text)
110 if (location.Equals (Location.Null)) {
111 Print (code, "", text);
114 Print (code, String.Format ("{0}({1})", location.Name, location.Row), text);
118 sealed class WarningMessage: AbstractMessage {
119 Location loc = Location.Null;
122 public WarningMessage ():
125 public WarningMessage (int level)
130 bool IsEnabled (int code)
132 if (RootContext.WarningLevel < Level)
135 if (warning_ignore_table != null) {
136 if (warning_ignore_table.Contains (code)) {
141 if (warning_regions_table == null || loc.Equals (Location.Null))
144 WarningRegions regions = (WarningRegions)warning_regions_table [loc.Name];
145 return regions.IsWarningEnabled (code, loc.Row);
148 public override void Print(int code, string location, string text)
150 if (!IsEnabled (code)) {
151 related_symbols.Clear ();
155 if (WarningsAreErrors) {
156 new ErrorMessage ().Print (code, location, text);
161 base.Print (code, location, text);
164 public override void Print(int code, Location location, string text)
167 base.Print (code, location, text);
170 public override string MessageType {
177 sealed class ErrorMessage: AbstractMessage {
179 public override void Print(int code, string location, string text)
182 base.Print (code, location, text);
185 public override string MessageType {
193 public static void FeatureIsNotStandardized (Location loc, string feature)
195 Report.Error (1644, loc, "Feature '{0}' cannot be used because it is not part of the standardized ISO C# language specification", feature);
198 public static string FriendlyStackTrace (Exception e)
200 return FriendlyStackTrace (new StackTrace (e, true));
203 static string FriendlyStackTrace (StackTrace t)
205 StringBuilder sb = new StringBuilder ();
207 bool foundUserCode = false;
209 for (int i = 0; i < t.FrameCount; i++) {
210 StackFrame f = t.GetFrame (i);
211 MethodBase mb = f.GetMethod ();
213 if (!foundUserCode && mb.ReflectedType == typeof (Report))
216 foundUserCode = true;
220 if (f.GetFileLineNumber () > 0)
221 sb.AppendFormat ("(at {0}:{1}) ", f.GetFileName (), f.GetFileLineNumber ());
223 sb.AppendFormat ("{0}.{1} (", mb.ReflectedType.Name, mb.Name);
226 foreach (ParameterInfo pi in mb.GetParameters ()) {
231 sb.Append (TypeManager.CSharpName (pi.ParameterType));
236 return sb.ToString ();
239 // !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
240 // IF YOU ADD A NEW WARNING YOU HAVE TO DUPLICATE ITS ID HERE
241 public static bool IsValidWarning (int code)
243 int[] all_warnings = new int[] { 28, 67, 78, 105, 108, 109, 114, 192, 168, 169, 183, 184, 219, 251, 612, 618, 626, 628, 642, 649,
244 659, 660, 661, 672, 1030, 1522, 1616, 1691, 1692, 1901, 2002, 2023, 3012, 3019, 8024, 8028
246 foreach (int i in all_warnings) {
253 static public void LocationOfPreviousError (Location loc)
255 Console.WriteLine (String.Format ("{0}({1}) (Location of symbol related to previous error)", loc.Name, loc.Row));
258 static public void RuntimeMissingSupport (string feature)
260 Report.Error (-88, "Your .NET Runtime does not support '{0}'. Please use the latest Mono runtime instead.", feature);
264 /// In most error cases is very useful to have information about symbol that caused the error.
265 /// Call this method before you call Report.Error when it makes sense.
267 static public void SymbolRelatedToPreviousError (Location loc, string symbol)
269 SymbolRelatedToPreviousError (String.Format ("{0}({1})", loc.Name, loc.Row), symbol);
272 static public void SymbolRelatedToPreviousError (MemberInfo mi)
274 TypeContainer temp_ds = TypeManager.LookupTypeContainer (mi.DeclaringType);
275 if (temp_ds == null) {
276 SymbolRelatedToPreviousError (mi.DeclaringType.Assembly.Location, TypeManager.GetFullNameSignature (mi));
278 if (mi is MethodBase) {
279 IMethodData md = TypeManager.GetMethod ((MethodBase)mi);
280 SymbolRelatedToPreviousError (md.Location, md.GetSignatureForError (temp_ds));
284 string name = String.Concat (temp_ds.Name, ".", mi.Name);
285 MemberCore mc = temp_ds.GetDefinition (name);
286 SymbolRelatedToPreviousError (mc);
290 static public void SymbolRelatedToPreviousError (MemberCore mc)
292 SymbolRelatedToPreviousError (mc.Location, mc.GetSignatureForError ());
295 static public void SymbolRelatedToPreviousError (Type type)
297 DeclSpace temp_ds = TypeManager.LookupDeclSpace (type);
299 SymbolRelatedToPreviousError (type.Assembly.Location, TypeManager.CSharpName (type));
301 SymbolRelatedToPreviousError (temp_ds.Location, TypeManager.CSharpName (type));
304 static void SymbolRelatedToPreviousError (string loc, string symbol)
306 related_symbols.Add (String.Format ("{0}: '{1}' (name of symbol related to previous ", loc, symbol));
309 public static WarningRegions RegisterWarningRegion (Location location)
311 if (warning_regions_table == null)
312 warning_regions_table = new Hashtable ();
314 WarningRegions regions = (WarningRegions)warning_regions_table [location.Name];
315 if (regions == null) {
316 regions = new WarningRegions ();
317 warning_regions_table.Add (location.Name, regions);
322 static public void Warning (int code, int level, Location loc, string format, params object[] args)
324 WarningMessage w = new WarningMessage (level);
325 w.Print (code, loc, String.Format (format, args));
328 static public void Warning (int code, Location loc, string format, params object[] args)
330 WarningMessage w = new WarningMessage ();
331 w.Print (code, loc, String.Format (format, args));
334 static public void Warning (int code, string format, params object[] args)
336 Warning (code, Location.Null, String.Format (format, args));
340 /// Did you test your WarningLevel, that you use this method
342 static public void Warning (int code, string text)
344 Warning (code, Location.Null, text);
347 static public void Error (int code, string format, params object[] args)
349 Error (code, Location.Null, String.Format (format, args));
352 static public void Error (int code, Location loc, string format, params object[] args)
354 ErrorMessage e = new ErrorMessage ();
355 e.Print (code, loc, String.Format (format, args));
358 static public void SetIgnoreWarning (int code)
360 if (warning_ignore_table == null)
361 warning_ignore_table = new Hashtable ();
363 warning_ignore_table [code] = true;
366 static public int ExpectedError {
368 expected_error = value;
371 return expected_error;
375 public static int DebugFlags = 0;
377 [Conditional ("MCS_DEBUG")]
378 static public void Debug (string message, params object[] args)
380 Debug (4, message, args);
383 [Conditional ("MCS_DEBUG")]
384 static public void Debug (int category, string message, params object[] args)
386 if ((category & DebugFlags) == 0)
389 StringBuilder sb = new StringBuilder (message);
391 if ((args != null) && (args.Length > 0)) {
395 foreach (object arg in args) {
402 else if (arg is ICollection)
403 sb.Append (PrintCollection ((ICollection) arg));
409 Console.WriteLine (sb.ToString ());
412 static public string PrintCollection (ICollection collection)
414 StringBuilder sb = new StringBuilder ();
416 sb.Append (collection.GetType ());
420 foreach (object o in collection) {
429 return sb.ToString ();
433 public enum TimerType {
443 public enum CounterType {
452 static DateTime[] timer_start;
453 static TimeSpan[] timers;
454 static long[] timer_counters;
455 static long[] counters;
459 timer_start = new DateTime [(int) TimerType.CountTimers];
460 timers = new TimeSpan [(int) TimerType.CountTimers];
461 timer_counters = new long [(int) TimerType.CountTimers];
462 counters = new long [(int) CounterType.CountCounters];
464 for (int i = 0; i < (int) TimerType.CountTimers; i++) {
465 timer_start [i] = DateTime.Now;
466 timers [i] = TimeSpan.Zero;
470 [Conditional("TIMER")]
471 static public void IncrementCounter (CounterType which)
473 ++counters [(int) which];
476 [Conditional("TIMER")]
477 static public void StartTimer (TimerType which)
479 timer_start [(int) which] = DateTime.Now;
482 [Conditional("TIMER")]
483 static public void StopTimer (TimerType which)
485 timers [(int) which] += DateTime.Now - timer_start [(int) which];
486 ++timer_counters [(int) which];
489 [Conditional("TIMER")]
490 static public void ShowTimers ()
492 ShowTimer (TimerType.FindMembers, "- FindMembers timer");
493 ShowTimer (TimerType.TcFindMembers, "- TypeContainer.FindMembers timer");
494 ShowTimer (TimerType.MemberLookup, "- MemberLookup timer");
495 ShowTimer (TimerType.CachedLookup, "- CachedLookup timer");
496 ShowTimer (TimerType.CacheInit, "- Cache init");
497 ShowTimer (TimerType.MiscTimer, "- Misc timer");
499 ShowCounter (CounterType.FindMembers, "- Find members");
500 ShowCounter (CounterType.MemberCache, "- Member cache");
501 ShowCounter (CounterType.MiscCounter, "- Misc counter");
504 static public void ShowCounter (CounterType which, string msg)
506 Console.WriteLine ("{0} {1}", counters [(int) which], msg);
509 static public void ShowTimer (TimerType which, string msg)
512 "[{0:00}:{1:000}] {2} (used {3} times)",
513 (int) timers [(int) which].TotalSeconds,
514 timers [(int) which].Milliseconds, msg,
515 timer_counters [(int) which]);
519 public class InternalErrorException : Exception {
520 public InternalErrorException ()
521 : base ("Internal error")
525 public InternalErrorException (string message)
532 /// Handles #pragma warning
534 public class WarningRegions {
536 abstract class PragmaCmd
540 protected PragmaCmd (int line)
545 public abstract bool IsEnabled (int code, bool previous);
548 class Disable: PragmaCmd
551 public Disable (int line, int code)
557 public override bool IsEnabled (int code, bool previous)
559 return this.code == code ? false : previous;
563 class DisableAll: PragmaCmd
565 public DisableAll (int line)
568 public override bool IsEnabled(int code, bool previous)
574 class Enable: PragmaCmd
577 public Enable (int line, int code)
583 public override bool IsEnabled(int code, bool previous)
585 return this.code == code ? true : previous;
589 class EnableAll: PragmaCmd
591 public EnableAll (int line)
594 public override bool IsEnabled(int code, bool previous)
601 ArrayList regions = new ArrayList ();
603 public void WarningDisable (int line)
605 regions.Add (new DisableAll (line));
608 public void WarningDisable (Location location, int code)
610 if (CheckWarningCode (code, location))
611 regions.Add (new Disable (location.Row, code));
614 public void WarningEnable (int line)
616 regions.Add (new EnableAll (line));
619 public void WarningEnable (Location location, int code)
621 if (CheckWarningCode (code, location))
622 regions.Add (new Enable (location.Row, code));
625 public bool IsWarningEnabled (int code, int src_line)
628 foreach (PragmaCmd pragma in regions) {
629 if (src_line < pragma.Line)
632 result = pragma.IsEnabled (code, result);
637 bool CheckWarningCode (int code, Location loc)
639 if (Report.IsValidWarning (code))
642 Report.Warning (1691, 1, loc, "'{0}' is not a valid warning number", code);