2002-11-16 Martin Baulig <martin@ximian.com>
[mono.git] / mcs / mcs / report.cs
index 5626b656c9e13389e1793d9ea625f9355d2ae2df..2a3fc301f7b7fc8f3489ecba22acbfa4034536e8 100644 (file)
@@ -6,8 +6,13 @@
 // (C) 2001 Ximian, Inc. (http://www.ximian.com)
 //
 
+//
+// FIXME: currently our class library does not support custom number format strings
+//
 using System;
+using System.Text;
 using System.Collections;
+using System.Diagnostics;
 
 namespace Mono.CSharp {
 
@@ -15,24 +20,38 @@ namespace Mono.CSharp {
        ///   This class is used to report errors and warnings t te user.
        /// </summary>
        public class Report {
-               static int errors;
-               static int warnings;
+               /// <summary>  
+               ///   Errors encountered so far
+               /// </summary>
+               static public int Errors;
 
-               // whether errors are fatal (they throw an exception), useful
-               // for debugging the compiler
-               static bool fatal;
+               /// <summary>  
+               ///   Warnings encountered so far
+               /// </summary>
+               static public int Warnings;
 
-               // whether we consider warnings to be errors.
-               static bool warnings_are_errors;
+               /// <summary>  
+               ///   Whether errors should be throw an exception
+               /// </summary>
+               static public bool Fatal;
+               
+               /// <summary>  
+               ///   Whether warnings should be considered errors
+               /// </summary>
+               static public bool WarningsAreErrors;
+
+               /// <summary>  
+               ///   Whether to dump a stack trace on errors. 
+               /// </summary>
+               static public bool Stacktrace;
                
                //
-               // If the error code is reported on the given line,
-               // then the process exits with a unique error code.
+               // If the 'expected' error code is reported then the
+                // compilation succeeds.
                //
                // Used for the test suite to excercise the error codes
                //
-               static int probe_error = 0;
-               static int probe_line = 0;
+               static int expected_error = 0;
 
                //
                // Keeps track of the warnings that we are ignoring
@@ -41,25 +60,31 @@ namespace Mono.CSharp {
                
                static void Check (int code)
                {
-                       if (code ==  probe_error){
-                               Environment.Exit (123);
+                       if (code == expected_error){
+                               if (Fatal)
+                                       throw new Exception ();
+                               
+                               Environment.Exit (0);
                        }
                }
                
                static public void RealError (string msg)
                {
-                       errors++;
+                       Errors++;
                        Console.WriteLine (msg);
 
-                       if (fatal)
+                       if (Stacktrace)
+                               Console.WriteLine (new StackTrace ().ToString ());
+                       if (Fatal)
                                throw new Exception (msg);
                }
-                      
+
                static public void Error (int code, Location l, string text)
                {
-                       string msg = l.Name + "(" + l.Row + 
-                               "): error CS"+code+": " + text;
-
+                       string msg = String.Format (
+                               "{0}({1}) error CS{2:0000}: {3}", l.Name, l.Row, code, text);
+//                             "{0}({1}) error CS{2}: {3}", l.Name, l.Row, code, text);
+                       
                        RealError (msg);
                        Check (code);
                }
@@ -71,7 +96,7 @@ namespace Mono.CSharp {
                                        return;
                        }
                        
-                       if (warnings_are_errors)
+                       if (WarningsAreErrors)
                                Error (code, l, text);
                        else {
                                string row;
@@ -81,9 +106,15 @@ namespace Mono.CSharp {
                                else
                                        row = l.Row.ToString ();
                                
-                               Console.WriteLine (l.Name + "(" + row + "): warning CS"+code+": " + text);
-                               warnings++;
+                               Console.WriteLine (String.Format (
+                                       "{0}({1}) warning CS{2:0000}: {3}",
+//                                     "{0}({1}) warning CS{2}: {3}",
+                                       l.Name,  row, code, text));
+                               Warnings++;
                                Check (code);
+
+                               if (Stacktrace)
+                                       Console.WriteLine (new StackTrace ().ToString ());
                        }
                }
                
@@ -92,10 +123,23 @@ namespace Mono.CSharp {
                        Warning (code, Location.Null, text);
                }
 
-               static public void Error (int code, string text)
+               static public void Warning (int code, int level, string text)
                {
-                       string msg = "error CS"+code+": "+text;
+                       if (RootContext.WarningLevel >= level)
+                               Warning (code, Location.Null, text);
+               }
+
+               static public void Warning (int code, int level, Location l, string text)
+               {
+                       if (RootContext.WarningLevel >= level)
+                               Warning (code, l, text);
+               }
 
+               static public void Error (int code, string text)
+               {
+                       string msg = String.Format ("error CS{0:0000}: {1}", code, text);
+//                     string msg = String.Format ("error CS{0}: {1}", code, text);
+                       
                        RealError (msg);
                        Check (code);
                }
@@ -116,44 +160,70 @@ namespace Mono.CSharp {
                        warning_ignore_table [code] = true;
                }
                
-               static public void SetProbe (int code, int line)
+                static public int ExpectedError {
+                        set {
+                                expected_error = value;
+                        }
+                        get {
+                                return expected_error;
+                        }
+                }
+
+               public static int DebugFlags = 0;
+
+               [Conditional ("MCS_DEBUG")]
+               static public void Debug (string message, params object[] args)
                {
-                       probe_error = code;
-                       probe_line = line;
+                       Debug (4, message, args);
                }
+                       
+               [Conditional ("MCS_DEBUG")]
+               static public void Debug (int category, string message, params object[] args)
+               {
+                       if ((category & DebugFlags) == 0)
+                               return;
 
-               static public int ProbeCode {
-                       get {
-                               return probe_error;
-                       }
-               }
-               
-               static public int Errors {
-                       get {
-                               return errors;
-                       }
-               }
+                       StringBuilder sb = new StringBuilder (message);
 
-               static public int Warnings {
-                       get {
-                               return warnings;
-                       }
-               }
+                       if ((args != null) && (args.Length > 0)) {
+                               sb.Append (": ");
 
-               static public bool Fatal {
-                       set {
-                               fatal = true;
+                               bool first = true;
+                               foreach (object arg in args) {
+                                       if (first)
+                                               first = false;
+                                       else
+                                               sb.Append (", ");
+                                       if (arg == null)
+                                               sb.Append ("null");
+                                       else if (arg is ICollection)
+                                               sb.Append (PrintCollection ((ICollection) arg));
+                                       else
+                                               sb.Append (arg);
+                               }
                        }
 
-                       get {
-                               return fatal;
-                       }
+                       Console.WriteLine (sb.ToString ());
                }
 
-               static public bool WarningsAreErrors {
-                       set {
-                               warnings_are_errors = true;
+               static public string PrintCollection (ICollection collection)
+               {
+                       StringBuilder sb = new StringBuilder ();
+
+                       sb.Append (collection.GetType ());
+                       sb.Append ("(");
+
+                       bool first = true;
+                       foreach (object o in collection) {
+                               if (first)
+                                       first = false;
+                               else
+                                       sb.Append (", ");
+                               sb.Append (o);
                        }
+
+                       sb.Append (")");
+                       return sb.ToString ();
                }
        }
 
@@ -188,6 +258,102 @@ namespace Mono.CSharp {
                        Console.WriteLine (error);
                }
        }
-}
 
+       public enum TimerType {
+               FindMembers     = 0,
+               TcFindMembers   = 1,
+               MemberLookup    = 2,
+               CachedLookup    = 3,
+               CacheInit       = 4,
+               MiscTimer       = 5,
+               CountTimers     = 6
+       }
 
+       public enum CounterType {
+               FindMembers     = 0,
+               MemberCache     = 1,
+               MiscCounter     = 2,
+               CountCounters   = 3
+       }
+
+       public class Timer
+       {
+               static DateTime[] timer_start;
+               static TimeSpan[] timers;
+               static long[] timer_counters;
+               static long[] counters;
+
+               static Timer ()
+               {
+                       timer_start = new DateTime [(int) TimerType.CountTimers];
+                       timers = new TimeSpan [(int) TimerType.CountTimers];
+                       timer_counters = new long [(int) TimerType.CountTimers];
+                       counters = new long [(int) CounterType.CountCounters];
+
+                       for (int i = 0; i < (int) TimerType.CountTimers; i++) {
+                               timer_start [i] = DateTime.Now;
+                               timers [i] = TimeSpan.Zero;
+                       }
+               }
+
+               [Conditional("TIMER")]
+               static public void IncrementCounter (CounterType which)
+               {
+                       ++counters [(int) which];
+               }
+
+               [Conditional("TIMER")]
+               static public void StartTimer (TimerType which)
+               {
+                       timer_start [(int) which] = DateTime.Now;
+               }
+
+               [Conditional("TIMER")]
+               static public void StopTimer (TimerType which)
+               {
+                       timers [(int) which] += DateTime.Now - timer_start [(int) which];
+                       ++timer_counters [(int) which];
+               }
+
+               [Conditional("TIMER")]
+               static public void ShowTimers ()
+               {
+                       ShowTimer (TimerType.FindMembers, "- FindMembers timer");
+                       ShowTimer (TimerType.TcFindMembers, "- TypeContainer.FindMembers timer");
+                       ShowTimer (TimerType.MemberLookup, "- MemberLookup timer");
+                       ShowTimer (TimerType.CachedLookup, "- CachedLookup timer");
+                       ShowTimer (TimerType.CacheInit, "- Cache init");
+                       ShowTimer (TimerType.MiscTimer, "- Misc timer");
+
+                       ShowCounter (CounterType.FindMembers, "- Find members");
+                       ShowCounter (CounterType.MemberCache, "- Member cache");
+                       ShowCounter (CounterType.MiscCounter, "- Misc counter");
+               }
+
+               static public void ShowCounter (CounterType which, string msg)
+               {
+                       Console.WriteLine ("{0} {1}", counters [(int) which], msg);
+               }
+
+               static public void ShowTimer (TimerType which, string msg)
+               {
+                       Console.WriteLine (
+                               "[{0:00}:{1:000}] {2} (used {3} times)",
+                               (int) timers [(int) which].TotalSeconds,
+                               timers [(int) which].Milliseconds, msg,
+                               timer_counters [(int) which]);
+               }
+       }
+
+       public class InternalErrorException : Exception {
+               public InternalErrorException ()
+                       : base ("Internal error")
+               {
+               }
+
+               public InternalErrorException (string message)
+                       : base (message)
+               {
+               }
+       }
+}