2003-10-29 Ben Maurer <bmaurer@users.sourceforge.net>
[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.CSharp {
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                                 string row;
123                                 
124                                 if (Location.IsNull (l))
125                                         Console.WriteLine(String.Format("{0} warning BC{2:0000}: {3}",
126                                                 l.Name, code, text));
127                                 else
128                                         Console.WriteLine(String.Format("{0}({1},{2}) warning BC{2:0000}: {3}",
129                                                 l.Name, l.Row, l.Col, code, text));
130                                 Warnings++;
131                                 Check (code);
132
133                                 if (Stacktrace)
134                                         Console.WriteLine (new StackTrace ().ToString ());
135                         }
136                 }
137                 
138                 static public void Warning (int code, string text)
139                 {
140                         Warning (code, Location.Null, text);
141                 }
142
143                 static public void Warning (int code, int level, string text)
144                 {
145                         if (RootContext.WarningLevel >= level)
146                                 Warning (code, Location.Null, text);
147                 }
148
149                 static public void Warning (int code, int level, Location l, string text)
150                 {
151                         if (RootContext.WarningLevel >= level)
152                                 Warning (code, l, text);
153                 }
154
155                 static public void Error (int code, string text)
156                 {
157                         string msg = String.Format ("error BC{0:0000}: {1}", code, text);
158                         
159                         RealError (msg);
160                         Check (code);
161                 }
162
163                 static public void Message (Message m)
164                 {
165                         if (m is ErrorMessage)
166                                 Error (m.code, m.text);
167                         else
168                                 Warning (m.code, m.text);
169                 }
170
171                 static public void SetIgnoreWarning (int code)
172                 {
173                         if (warning_ignore_table == null)
174                                 warning_ignore_table = new Hashtable ();
175
176                         warning_ignore_table [code] = true;
177                 }
178                 
179                 static public int ExpectedError {
180                         set {
181                                 expected_error = value;
182                         }
183                         get {
184                                 return expected_error;
185                         }
186                 }
187
188                 public static int DebugFlags = 0;
189
190                 [Conditional ("MCS_DEBUG")]
191                 static public void Debug (string message, params object[] args)
192                 {
193                         Debug (4, message, args);
194                 }
195                         
196                 [Conditional ("MCS_DEBUG")]
197                 static public void Debug (int category, string message, params object[] args)
198                 {
199                         if ((category & DebugFlags) == 0)
200                                 return;
201
202                         StringBuilder sb = new StringBuilder (message);
203
204                         if ((args != null) && (args.Length > 0)) {
205                                 sb.Append (": ");
206
207                                 bool first = true;
208                                 foreach (object arg in args) {
209                                         if (first)
210                                                 first = false;
211                                         else
212                                                 sb.Append (",");
213                                         if (arg == null)
214                                                 sb.Append ("null");
215                                         else if (arg is ICollection)
216                                                 sb.Append (PrintCollection ((ICollection) arg));
217                                         else
218                                                 sb.Append (arg);
219                                 }
220                         }
221
222                         Console.WriteLine (sb.ToString ());
223                 }
224
225                 static public string PrintCollection (ICollection collection)
226                 {
227                         StringBuilder sb = new StringBuilder ();
228
229                         sb.Append (collection.GetType ());
230                         sb.Append ("(");
231
232                         bool first = true;
233                         foreach (object o in collection) {
234                                 if (first)
235                                         first = false;
236                                 else
237                                         sb.Append (",");
238                                 sb.Append (o);
239                         }
240
241                         sb.Append (")");
242                         return sb.ToString ();
243                 }
244         }
245
246         public class Message {
247                 public int code;
248                 public string text;
249                 
250                 public Message (int code, string text)
251                 {
252                         this.code = code;
253                         this.text = text;
254                 }
255         }
256
257         public class WarningMessage : Message {
258                 public WarningMessage (int code, string text) : base (code, text)
259                 {
260                 }
261         }
262
263         public class ErrorMessage : Message {
264                 public ErrorMessage (int code, string text) : base (code, text)
265                 {
266                 }
267
268                 //
269                 // For compatibility reasons with old code.
270                 //
271                 public static void report_error (string error)
272                 {
273                         Console.Write ("ERROR: ");
274                         Console.WriteLine (error);
275                 }
276         }
277
278         public enum TimerType {
279                 FindMembers     = 0,
280                 TcFindMembers   = 1,
281                 MemberLookup    = 2,
282                 CachedLookup    = 3,
283                 CacheInit       = 4,
284                 MiscTimer       = 5,
285                 CountTimers     = 6
286         }
287
288         public enum CounterType {
289                 FindMembers     = 0,
290                 MemberCache     = 1,
291                 MiscCounter     = 2,
292                 CountCounters   = 3
293         }
294
295         public class Timer
296         {
297                 static DateTime[] timer_start;
298                 static TimeSpan[] timers;
299                 static long[] timer_counters;
300                 static long[] counters;
301
302                 static Timer ()
303                 {
304                         timer_start = new DateTime [(int) TimerType.CountTimers];
305                         timers = new TimeSpan [(int) TimerType.CountTimers];
306                         timer_counters = new long [(int) TimerType.CountTimers];
307                         counters = new long [(int) CounterType.CountCounters];
308
309                         for (int i = 0; i < (int) TimerType.CountTimers; i++) {
310                                 timer_start [i] = DateTime.Now;
311                                 timers [i] = TimeSpan.Zero;
312                         }
313                 }
314
315                 [Conditional("TIMER")]
316                 static public void IncrementCounter (CounterType which)
317                 {
318                         ++counters [(int) which];
319                 }
320
321                 [Conditional("TIMER")]
322                 static public void StartTimer (TimerType which)
323                 {
324                         timer_start [(int) which] = DateTime.Now;
325                 }
326
327                 [Conditional("TIMER")]
328                 static public void StopTimer (TimerType which)
329                 {
330                         timers [(int) which] += DateTime.Now - timer_start [(int) which];
331                         ++timer_counters [(int) which];
332                 }
333
334                 [Conditional("TIMER")]
335                 static public void ShowTimers ()
336                 {
337                         ShowTimer (TimerType.FindMembers, "- FindMembers timer");
338                         ShowTimer (TimerType.TcFindMembers, "- TypeContainer.FindMembers timer");
339                         ShowTimer (TimerType.MemberLookup, "- MemberLookup timer");
340                         ShowTimer (TimerType.CachedLookup, "- CachedLookup timer");
341                         ShowTimer (TimerType.CacheInit, "- Cache init");
342                         ShowTimer (TimerType.MiscTimer, "- Misc timer");
343
344                         ShowCounter (CounterType.FindMembers, "- Find members");
345                         ShowCounter (CounterType.MemberCache, "- Member cache");
346                         ShowCounter (CounterType.MiscCounter, "- Misc counter");
347                 }
348
349                 static public void ShowCounter (CounterType which, string msg)
350                 {
351                         Console.WriteLine ("{0} {1}", counters [(int) which], msg);
352                 }
353
354                 static public void ShowTimer (TimerType which, string msg)
355                 {
356                         Console.WriteLine (
357                                 "[{0:00}:{1:000}] {2} (used {3} times)",
358                                 (int) timers [(int) which].TotalSeconds,
359                                 timers [(int) which].Milliseconds, msg,
360                                 timer_counters [(int) which]);
361                 }
362         }
363
364         public class InternalErrorException : Exception {
365                 public InternalErrorException ()
366                         : base ("Internal error")
367                 {
368                 }
369
370                 public InternalErrorException (string message)
371                         : base (message)
372                 {
373                 }
374         }
375 }