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