2003-04-14 Gaurav Vaish <gvaish_mono AT lycos.com>
[mono.git] / mcs / mcs / 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 void Check (int code)
62                 {
63                         if (code == expected_error){
64                                 if (Fatal)
65                                         throw new Exception ();
66                                 
67                                 Environment.Exit (0);
68                         }
69                 }
70                 
71                 static public void RealError (string msg)
72                 {
73                         Errors++;
74                         Console.WriteLine (msg);
75
76                         if (Stacktrace)
77                                 Console.WriteLine (new StackTrace ().ToString ());
78                         if (Fatal)
79                                 throw new Exception (msg);
80                 }
81
82                 static public void Error (int code, Location l, string text)
83                 {
84                         string msg = String.Format (
85                                 "{0}({1}) error CS{2:0000}: {3}", l.Name, l.Row, code, text);
86 //                              "{0}({1}) error CS{2}: {3}", l.Name, l.Row, code, text);
87                         
88                         RealError (msg);
89                         Check (code);
90                 }
91
92                 static public void Warning (int code, Location l, string text)
93                 {
94                         if (warning_ignore_table != null){
95                                 if (warning_ignore_table.Contains (code))
96                                         return;
97                         }
98                         
99                         if (WarningsAreErrors)
100                                 Error (code, l, text);
101                         else {
102                                 string row;
103                                 
104                                 if (Location.IsNull (l))
105                                         row = "";
106                                 else
107                                         row = l.Row.ToString ();
108                                 
109                                 Console.WriteLine (String.Format (
110                                         "{0}({1}) warning CS{2:0000}: {3}",
111 //                                      "{0}({1}) warning CS{2}: {3}",
112                                         l.Name,  row, code, text));
113                                 Warnings++;
114                                 Check (code);
115
116                                 if (Stacktrace)
117                                         Console.WriteLine (new StackTrace ().ToString ());
118                         }
119                 }
120                 
121                 static public void Warning (int code, string text)
122                 {
123                         Warning (code, Location.Null, text);
124                 }
125
126                 static public void Warning (int code, int level, string text)
127                 {
128                         if (RootContext.WarningLevel >= level)
129                                 Warning (code, Location.Null, text);
130                 }
131
132                 static public void Warning (int code, int level, Location l, string text)
133                 {
134                         if (RootContext.WarningLevel >= level)
135                                 Warning (code, l, text);
136                 }
137
138                 static public void Error (int code, string text)
139                 {
140                         string msg = String.Format ("error CS{0:0000}: {1}", code, text);
141 //                      string msg = String.Format ("error CS{0}: {1}", code, text);
142                         
143                         RealError (msg);
144                         Check (code);
145                 }
146
147                 static public void Message (Message m)
148                 {
149                         if (m is ErrorMessage)
150                                 Error (m.code, m.text);
151                         else
152                                 Warning (m.code, m.text);
153                 }
154
155                 static public void SetIgnoreWarning (int code)
156                 {
157                         if (warning_ignore_table == null)
158                                 warning_ignore_table = new Hashtable ();
159
160                         warning_ignore_table [code] = true;
161                 }
162                 
163                 static public int ExpectedError {
164                         set {
165                                 expected_error = value;
166                         }
167                         get {
168                                 return expected_error;
169                         }
170                 }
171
172                 public static int DebugFlags = 0;
173
174                 [Conditional ("MCS_DEBUG")]
175                 static public void Debug (string message, params object[] args)
176                 {
177                         Debug (4, message, args);
178                 }
179                         
180                 [Conditional ("MCS_DEBUG")]
181                 static public void Debug (int category, string message, params object[] args)
182                 {
183                         if ((category & DebugFlags) == 0)
184                                 return;
185
186                         StringBuilder sb = new StringBuilder (message);
187
188                         if ((args != null) && (args.Length > 0)) {
189                                 sb.Append (": ");
190
191                                 bool first = true;
192                                 foreach (object arg in args) {
193                                         if (first)
194                                                 first = false;
195                                         else
196                                                 sb.Append (", ");
197                                         if (arg == null)
198                                                 sb.Append ("null");
199                                         else if (arg is ICollection)
200                                                 sb.Append (PrintCollection ((ICollection) arg));
201                                         else
202                                                 sb.Append (arg);
203                                 }
204                         }
205
206                         Console.WriteLine (sb.ToString ());
207                 }
208
209                 static public string PrintCollection (ICollection collection)
210                 {
211                         StringBuilder sb = new StringBuilder ();
212
213                         sb.Append (collection.GetType ());
214                         sb.Append ("(");
215
216                         bool first = true;
217                         foreach (object o in collection) {
218                                 if (first)
219                                         first = false;
220                                 else
221                                         sb.Append (", ");
222                                 sb.Append (o);
223                         }
224
225                         sb.Append (")");
226                         return sb.ToString ();
227                 }
228         }
229
230         public class Message {
231                 public int code;
232                 public string text;
233                 
234                 public Message (int code, string text)
235                 {
236                         this.code = code;
237                         this.text = text;
238                 }
239         }
240
241         public class WarningMessage : Message {
242                 public WarningMessage (int code, string text) : base (code, text)
243                 {
244                 }
245         }
246
247         public class ErrorMessage : Message {
248                 public ErrorMessage (int code, string text) : base (code, text)
249                 {
250                 }
251
252                 //
253                 // For compatibility reasons with old code.
254                 //
255                 public static void report_error (string error)
256                 {
257                         Console.Write ("ERROR: ");
258                         Console.WriteLine (error);
259                 }
260         }
261
262         public enum TimerType {
263                 FindMembers     = 0,
264                 TcFindMembers   = 1,
265                 MemberLookup    = 2,
266                 CachedLookup    = 3,
267                 CacheInit       = 4,
268                 MiscTimer       = 5,
269                 CountTimers     = 6
270         }
271
272         public enum CounterType {
273                 FindMembers     = 0,
274                 MemberCache     = 1,
275                 MiscCounter     = 2,
276                 CountCounters   = 3
277         }
278
279         public class Timer
280         {
281                 static DateTime[] timer_start;
282                 static TimeSpan[] timers;
283                 static long[] timer_counters;
284                 static long[] counters;
285
286                 static Timer ()
287                 {
288                         timer_start = new DateTime [(int) TimerType.CountTimers];
289                         timers = new TimeSpan [(int) TimerType.CountTimers];
290                         timer_counters = new long [(int) TimerType.CountTimers];
291                         counters = new long [(int) CounterType.CountCounters];
292
293                         for (int i = 0; i < (int) TimerType.CountTimers; i++) {
294                                 timer_start [i] = DateTime.Now;
295                                 timers [i] = TimeSpan.Zero;
296                         }
297                 }
298
299                 [Conditional("TIMER")]
300                 static public void IncrementCounter (CounterType which)
301                 {
302                         ++counters [(int) which];
303                 }
304
305                 [Conditional("TIMER")]
306                 static public void StartTimer (TimerType which)
307                 {
308                         timer_start [(int) which] = DateTime.Now;
309                 }
310
311                 [Conditional("TIMER")]
312                 static public void StopTimer (TimerType which)
313                 {
314                         timers [(int) which] += DateTime.Now - timer_start [(int) which];
315                         ++timer_counters [(int) which];
316                 }
317
318                 [Conditional("TIMER")]
319                 static public void ShowTimers ()
320                 {
321                         ShowTimer (TimerType.FindMembers, "- FindMembers timer");
322                         ShowTimer (TimerType.TcFindMembers, "- TypeContainer.FindMembers timer");
323                         ShowTimer (TimerType.MemberLookup, "- MemberLookup timer");
324                         ShowTimer (TimerType.CachedLookup, "- CachedLookup timer");
325                         ShowTimer (TimerType.CacheInit, "- Cache init");
326                         ShowTimer (TimerType.MiscTimer, "- Misc timer");
327
328                         ShowCounter (CounterType.FindMembers, "- Find members");
329                         ShowCounter (CounterType.MemberCache, "- Member cache");
330                         ShowCounter (CounterType.MiscCounter, "- Misc counter");
331                 }
332
333                 static public void ShowCounter (CounterType which, string msg)
334                 {
335                         Console.WriteLine ("{0} {1}", counters [(int) which], msg);
336                 }
337
338                 static public void ShowTimer (TimerType which, string msg)
339                 {
340                         Console.WriteLine (
341                                 "[{0:00}:{1:000}] {2} (used {3} times)",
342                                 (int) timers [(int) which].TotalSeconds,
343                                 timers [(int) which].Milliseconds, msg,
344                                 timer_counters [(int) which]);
345                 }
346         }
347
348         public class InternalErrorException : Exception {
349                 public InternalErrorException ()
350                         : base ("Internal error")
351                 {
352                 }
353
354                 public InternalErrorException (string message)
355                         : base (message)
356                 {
357                 }
358         }
359 }