2006-08-17 Atsushi Enomoto <atsushi@ximian.com>
[mono.git] / mcs / mbas / report.cs
1 //
2 // report.cs: report errors and warnings.
3 //
4 // Author: Miguel de Icaza (miguel@ximian.com)
5 //
6 // (C) 2001 Ximian, Inc. (http://www.ximian.com)
7 //
8
9 //
10 // FIXME: currently our class library does not support custom number format strings
11 //
12 using System;
13 using System.Text;
14 using System.Collections;
15 using System.Diagnostics;
16
17 namespace Mono.MonoBASIC {
18
19         /// <summary>
20         ///   This class is used to report errors and warnings t te user.
21         /// </summary>
22         public class Report {
23                 /// <summary>  
24                 ///   Errors encountered so far
25                 /// </summary>
26                 static public int Errors;
27
28                 /// <summary>  
29                 ///   Warnings encountered so far
30                 /// </summary>
31                 static public int Warnings;
32
33                 /// <summary>  
34                 ///   Whether errors should be throw an exception
35                 /// </summary>
36                 static public bool Fatal;
37                 
38                 /// <summary>  
39                 ///   Whether warnings should be considered errors
40                 /// </summary>
41                 static public bool WarningsAreErrors;
42
43                 /// <summary>  
44                 ///   Whether to dump a stack trace on errors. 
45                 /// </summary>
46                 static public bool Stacktrace;
47                 
48                 //
49                 // If the 'expected' error code is reported then the
50                 // compilation succeeds.
51                 //
52                 // Used for the test suite to excercise the error codes
53                 //
54                 static int expected_error = 0;
55
56                 //
57                 // Keeps track of the warnings that we are ignoring
58                 //
59                 static Hashtable warning_ignore_table;
60                 
61                 static public int ProcessResults(bool quiet)
62                 {
63                         if (!quiet)
64                         {
65                                 if (Report.ExpectedError != 0)
66                                         Console.WriteLine("Failed to report expected Error " + Report.ExpectedError);
67                                         
68                                 if (Errors == 0) 
69                                 {
70                                         if (Warnings == 0) 
71                                                 Console.WriteLine("Compilation succeeded");
72                                         else
73                                                 Console.WriteLine("Compilation succeeded: {0} warning(s)", Warnings);
74                                 }
75                                 else
76                                         Console.WriteLine("Compilation failed: {0} Error(s), {1} warnings",     Errors, Warnings);
77                         }
78                         return (Errors == 0)?0:1;
79                 }
80                 
81                 static void Check (int code)
82                 {
83                         if (code == expected_error)
84                         {
85                                 if (Fatal)
86                                         throw new Exception ();
87                                 
88                                 Environment.Exit (0);
89                         }
90                 }
91                 
92                 static private void RealError (string msg)
93                 {
94                         Errors++;
95                         Console.WriteLine (msg);
96
97                         if (Stacktrace)
98                                 Console.WriteLine (new StackTrace ().ToString ());
99                         if (Fatal)
100                                 throw new Exception (msg);
101                 }
102
103                 static public void Error (int code, Location l, string text)
104                 {
105                         string msg = String.Format (
106                                 "{0}({1},{2}) error BC{3:0000}: {4}", l.Name, l.Row, l.Col, code, text);
107                         
108                         RealError (msg);
109                         Check (code);
110                 }
111
112                 static public void Warning (int code, Location l, string text)
113                 {
114                         if (warning_ignore_table != null){
115                                 if (warning_ignore_table.Contains (code))
116                                         return;
117                         }
118                         
119                         if (WarningsAreErrors)
120                                 Error (code, l, text);
121                         else {
122                                 if (Location.IsNull (l))
123                                         Console.WriteLine(String.Format("{0} warning BC{1:0000}: {2}",
124                                                 l.Name, code, text));
125                                 else
126                                         Console.WriteLine(String.Format("{0}({1},{2}) warning BC{3:0000}: {4}",
127                                                 l.Name, l.Row, l.Col, code, text));
128                                 Warnings++;
129                                 Check (code);
130
131                                 if (Stacktrace)
132                                         Console.WriteLine (new StackTrace ().ToString ());
133                         }
134                 }
135                 
136                 static public void Warning (int code, string text)
137                 {
138                         Warning (code, Location.Null, text);
139                 }
140
141                 static public void Warning (int code, int level, string text)
142                 {
143                         if (RootContext.WarningLevel >= level)
144                                 Warning (code, Location.Null, text);
145                 }
146
147                 static public void Warning (int code, int level, Location l, string text)
148                 {
149                         if (RootContext.WarningLevel >= level)
150                                 Warning (code, l, text);
151                 }
152
153                 static public void Error (int code, string text)
154                 {
155                         string msg = String.Format ("error BC{0:0000}: {1}", code, text);
156                         
157                         RealError (msg);
158                         Check (code);
159                 }
160
161                 static public void Message (Message m)
162                 {
163                         if (m is ErrorMessage)
164                                 Error (m.code, m.text);
165                         else
166                                 Warning (m.code, m.text);
167                 }
168
169                 static public void SetIgnoreWarning (int code)
170                 {
171                         if (warning_ignore_table == null)
172                                 warning_ignore_table = new Hashtable ();
173
174                         warning_ignore_table [code] = true;
175                 }
176                 
177                 static public int ExpectedError {
178                         set {
179                                 expected_error = value;
180                         }
181                         get {
182                                 return expected_error;
183                         }
184                 }
185
186                 public static int DebugFlags = 0;
187
188                 [Conditional ("MCS_DEBUG")]
189                 static public void Debug (string message, params object[] args)
190                 {
191                         Debug (4, message, args);
192                 }
193                         
194                 [Conditional ("MCS_DEBUG")]
195                 static public void Debug (int category, string message, params object[] args)
196                 {
197                         if ((category & DebugFlags) == 0)
198                                 return;
199
200                         StringBuilder sb = new StringBuilder (message);
201
202                         if ((args != null) && (args.Length > 0)) {
203                                 sb.Append (": ");
204
205                                 bool first = true;
206                                 foreach (object arg in args) {
207                                         if (first)
208                                                 first = false;
209                                         else
210                                                 sb.Append (",");
211                                         if (arg == null)
212                                                 sb.Append ("null");
213                                         else if (arg is ICollection)
214                                                 sb.Append (PrintCollection ((ICollection) arg));
215                                         else
216                                                 sb.Append (arg);
217                                 }
218                         }
219
220                         Console.WriteLine (sb.ToString ());
221                 }
222
223                 static public string PrintCollection (ICollection collection)
224                 {
225                         StringBuilder sb = new StringBuilder ();
226
227                         sb.Append (collection.GetType ());
228                         sb.Append ("(");
229
230                         bool first = true;
231                         foreach (object o in collection) {
232                                 if (first)
233                                         first = false;
234                                 else
235                                         sb.Append (",");
236                                 sb.Append (o);
237                         }
238
239                         sb.Append (")");
240                         return sb.ToString ();
241                 }
242         }
243
244         public class Message {
245                 public int code;
246                 public string text;
247                 
248                 public Message (int code, string text)
249                 {
250                         this.code = code;
251                         this.text = text;
252                 }
253         }
254
255         public class WarningMessage : Message {
256                 public WarningMessage (int code, string text) : base (code, text)
257                 {
258                 }
259         }
260
261         public class ErrorMessage : Message {
262                 public ErrorMessage (int code, string text) : base (code, text)
263                 {
264                 }
265
266                 //
267                 // For compatibility reasons with old code.
268                 //
269                 public static void report_error (string error)
270                 {
271                         Console.Write ("ERROR: ");
272                         Console.WriteLine (error);
273                 }
274         }
275
276         public enum TimerType {
277                 FindMembers     = 0,
278                 TcFindMembers   = 1,
279                 MemberLookup    = 2,
280                 CachedLookup    = 3,
281                 CacheInit       = 4,
282                 MiscTimer       = 5,
283                 CountTimers     = 6
284         }
285
286         public enum CounterType {
287                 FindMembers     = 0,
288                 MemberCache     = 1,
289                 MiscCounter     = 2,
290                 CountCounters   = 3
291         }
292
293         public class Timer
294         {
295                 static DateTime[] timer_start;
296                 static TimeSpan[] timers;
297                 static long[] timer_counters;
298                 static long[] counters;
299
300                 static Timer ()
301                 {
302                         timer_start = new DateTime [(int) TimerType.CountTimers];
303                         timers = new TimeSpan [(int) TimerType.CountTimers];
304                         timer_counters = new long [(int) TimerType.CountTimers];
305                         counters = new long [(int) CounterType.CountCounters];
306
307                         for (int i = 0; i < (int) TimerType.CountTimers; i++) {
308                                 timer_start [i] = DateTime.Now;
309                                 timers [i] = TimeSpan.Zero;
310                         }
311                 }
312
313                 [Conditional("TIMER")]
314                 static public void IncrementCounter (CounterType which)
315                 {
316                         ++counters [(int) which];
317                 }
318
319                 [Conditional("TIMER")]
320                 static public void StartTimer (TimerType which)
321                 {
322                         timer_start [(int) which] = DateTime.Now;
323                 }
324
325                 [Conditional("TIMER")]
326                 static public void StopTimer (TimerType which)
327                 {
328                         timers [(int) which] += DateTime.Now - timer_start [(int) which];
329                         ++timer_counters [(int) which];
330                 }
331
332                 [Conditional("TIMER")]
333                 static public void ShowTimers ()
334                 {
335                         ShowTimer (TimerType.FindMembers, "- FindMembers timer");
336                         ShowTimer (TimerType.TcFindMembers, "- TypeContainer.FindMembers timer");
337                         ShowTimer (TimerType.MemberLookup, "- MemberLookup timer");
338                         ShowTimer (TimerType.CachedLookup, "- CachedLookup timer");
339                         ShowTimer (TimerType.CacheInit, "- Cache init");
340                         ShowTimer (TimerType.MiscTimer, "- Misc timer");
341
342                         ShowCounter (CounterType.FindMembers, "- Find members");
343                         ShowCounter (CounterType.MemberCache, "- Member cache");
344                         ShowCounter (CounterType.MiscCounter, "- Misc counter");
345                 }
346
347                 static public void ShowCounter (CounterType which, string msg)
348                 {
349                         Console.WriteLine ("{0} {1}", counters [(int) which], msg);
350                 }
351
352                 static public void ShowTimer (TimerType which, string msg)
353                 {
354                         Console.WriteLine (
355                                 "[{0:00}:{1:000}] {2} (used {3} times)",
356                                 (int) timers [(int) which].TotalSeconds,
357                                 timers [(int) which].Milliseconds, msg,
358                                 timer_counters [(int) which]);
359                 }
360         }
361
362         public class InternalErrorException : Exception {
363                 public InternalErrorException ()
364                         : base ("Internal error")
365                 {
366                 }
367
368                 public InternalErrorException (string message)
369                         : base (message)
370                 {
371                 }
372         }
373 }