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