**** Merged from HEAD ****
[mono.git] / mcs / gmcs / 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 using System.Reflection;
17
18 namespace Mono.CSharp {
19
20         /// <summary>
21         ///   This class is used to report errors and warnings t te user.
22         /// </summary>
23         public class Report {
24                 /// <summary>  
25                 ///   Errors encountered so far
26                 /// </summary>
27                 static public int Errors;
28
29                 /// <summary>  
30                 ///   Warnings encountered so far
31                 /// </summary>
32                 static public int Warnings;
33
34                 /// <summary>  
35                 ///   Whether errors should be throw an exception
36                 /// </summary>
37                 static public bool Fatal;
38                 
39                 /// <summary>  
40                 ///   Whether warnings should be considered errors
41                 /// </summary>
42                 static public bool WarningsAreErrors;
43
44                 /// <summary>  
45                 ///   Whether to dump a stack trace on errors. 
46                 /// </summary>
47                 static public bool Stacktrace;
48                 
49                 //
50                 // If the 'expected' error code is reported then the
51                 // compilation succeeds.
52                 //
53                 // Used for the test suite to excercise the error codes
54                 //
55                 static int expected_error = 0;
56
57                 //
58                 // Keeps track of the warnings that we are ignoring
59                 //
60                 static Hashtable warning_ignore_table;
61                 
62                 static void Check (int code)
63                 {
64                         if (code == expected_error){
65                                 if (Fatal)
66                                         throw new Exception ();
67                                 
68                                 Environment.Exit (0);
69                         }
70                 }
71                 
72                 public static string FriendlyStackTrace (Exception e)
73                 {
74                         return FriendlyStackTrace (new StackTrace (e, true));
75                 }
76                 
77                 static string FriendlyStackTrace (StackTrace t)
78                 {               
79                         StringBuilder sb = new StringBuilder ();
80                         
81                         bool foundUserCode = false;
82                         
83                         for (int i = 0; i < t.FrameCount; i++) {
84                                 StackFrame f = t.GetFrame (i);
85                                 MethodBase mb = f.GetMethod ();
86                                 
87                                 if (!foundUserCode && mb.ReflectedType == typeof (Report))
88                                         continue;
89                                 
90                                 foundUserCode = true;
91                                 
92                                 sb.Append ("\tin ");
93                                 
94                                 if (f.GetFileLineNumber () > 0)
95                                         sb.AppendFormat ("(at {0}:{1}) ", f.GetFileName (), f.GetFileLineNumber ());
96                                 
97                                 sb.AppendFormat ("{0}.{1} (", mb.ReflectedType.Name, mb.Name);
98                                 
99                                 bool first = true;
100                                 foreach (ParameterInfo pi in mb.GetParameters ()) {
101                                         if (!first)
102                                                 sb.Append (", ");
103                                         first = false;
104                                         
105                                         sb.Append (TypeManager.CSharpName (pi.ParameterType));
106                                 }
107                                 sb.Append (")\n");
108                         }
109         
110                         return sb.ToString ();
111                 }
112                 
113                 static public void RealError (string msg)
114                 {
115                         Errors++;
116                         Console.WriteLine (msg);
117
118                         if (Stacktrace)
119                                 Console.WriteLine (FriendlyStackTrace (new StackTrace (true)));
120                         
121                         if (Fatal)
122                                 throw new Exception (msg);
123                 }
124
125                 static public void Error (int code, Location l, string text)
126                 {
127                         if (code < 0)
128                                 code = 8000-code;
129                         
130                         string msg = String.Format (
131                                 "{0}({1}) error CS{2:0000}: {3}", l.Name, l.Row, code, text);
132 //                              "{0}({1}) error CS{2}: {3}", l.Name, l.Row, code, text);
133                         
134                         RealError (msg);
135                         Check (code);
136                 }
137
138                 static public void Warning (int code, Location l, string text)
139                 {
140                         if (code < 0)
141                                 code = 8000-code;
142                         
143                         if (warning_ignore_table != null){
144                                 if (warning_ignore_table.Contains (code))
145                                         return;
146                         }
147                         
148                         if (WarningsAreErrors)
149                                 Error (code, l, text);
150                         else {
151                                 string row;
152                                 
153                                 if (Location.IsNull (l))
154                                         row = "";
155                                 else
156                                         row = l.Row.ToString ();
157                                 
158                                 Console.WriteLine (String.Format (
159                                         "{0}({1}) warning CS{2:0000}: {3}",
160 //                                      "{0}({1}) warning CS{2}: {3}",
161                                         l.Name,  row, code, text));
162                                 Warnings++;
163                                 Check (code);
164
165                                 if (Stacktrace)
166                                         Console.WriteLine (new StackTrace ().ToString ());
167                         }
168                 }
169                 
170                 static public void Warning (int code, string text)
171                 {
172                         Warning (code, Location.Null, text);
173                 }
174
175                 static public void Warning (int code, int level, string text)
176                 {
177                         if (RootContext.WarningLevel >= level)
178                                 Warning (code, Location.Null, text);
179                 }
180
181                 static public void Warning (int code, int level, Location l, string text)
182                 {
183                         if (RootContext.WarningLevel >= level)
184                                 Warning (code, l, text);
185                 }
186
187                 static public void Error (int code, string text)
188                 {
189                         if (code < 0)
190                                 code = 8000-code;
191                         
192                         string msg = String.Format ("error CS{0:0000}: {1}", code, text);
193 //                      string msg = String.Format ("error CS{0}: {1}", code, text);
194                         
195                         RealError (msg);
196                         Check (code);
197                 }
198
199                 static public void Error (int code, Location loc, string format, params object[] args)
200                 {
201                         Error (code, loc, String.Format (format, args));
202                 }
203
204                 static public void Error (int code, string format, params object[] args)
205                 {
206                         Error (code, String.Format (format, args));
207                 }
208
209                 static public void Warning (int code, Location loc, string format, params object[] args)
210                 {
211                         Warning (code, loc, String.Format (format, args));
212                 }
213
214                 static public void Warning (int code, string format, params object[] args)
215                 {
216                         Warning (code, String.Format (format, args));
217                 }
218
219                 static public void Message (Message m)
220                 {
221                         if (m is ErrorMessage)
222                                 Error (m.code, m.text);
223                         else
224                                 Warning (m.code, m.text);
225                 }
226
227                 static public void SetIgnoreWarning (int code)
228                 {
229                         if (warning_ignore_table == null)
230                                 warning_ignore_table = new Hashtable ();
231
232                         warning_ignore_table [code] = true;
233                 }
234                 
235                 static public int ExpectedError {
236                         set {
237                                 expected_error = value;
238                         }
239                         get {
240                                 return expected_error;
241                         }
242                 }
243
244                 public static int DebugFlags = 0;
245
246                 [Conditional ("MCS_DEBUG")]
247                 static public void Debug (string message, params object[] args)
248                 {
249                         Debug (4, message, args);
250                 }
251                         
252                 [Conditional ("MCS_DEBUG")]
253                 static public void Debug (int category, string message, params object[] args)
254                 {
255                         if ((category & DebugFlags) == 0)
256                                 return;
257
258                         StringBuilder sb = new StringBuilder (message);
259
260                         if ((args != null) && (args.Length > 0)) {
261                                 sb.Append (": ");
262
263                                 bool first = true;
264                                 foreach (object arg in args) {
265                                         if (first)
266                                                 first = false;
267                                         else
268                                                 sb.Append (", ");
269                                         if (arg == null)
270                                                 sb.Append ("null");
271                                         else if (arg is ICollection)
272                                                 sb.Append (PrintCollection ((ICollection) arg));
273                                         else if (arg is IntPtr)
274                                                 sb.Append (String.Format ("IntPtr(0x{0:x})", ((IntPtr) arg).ToInt32 ()));
275                                         else
276                                                 sb.Append (arg);
277                                 }
278                         }
279
280                         Console.WriteLine (sb.ToString ());
281                 }
282
283                 static public string PrintCollection (ICollection collection)
284                 {
285                         StringBuilder sb = new StringBuilder ();
286
287                         sb.Append (collection.GetType ());
288                         sb.Append ("(");
289
290                         bool first = true;
291                         foreach (object o in collection) {
292                                 if (first)
293                                         first = false;
294                                 else
295                                         sb.Append (", ");
296                                 sb.Append (o);
297                         }
298
299                         sb.Append (")");
300                         return sb.ToString ();
301                 }
302         }
303
304         public class Message {
305                 public int code;
306                 public string text;
307                 
308                 public Message (int code, string text)
309                 {
310                         this.code = code;
311                         this.text = text;
312                 }
313         }
314
315         public class WarningMessage : Message {
316                 public WarningMessage (int code, string text) : base (code, text)
317                 {
318                 }
319         }
320
321         public class ErrorMessage : Message {
322                 public ErrorMessage (int code, string text) : base (code, text)
323                 {
324                 }
325
326                 //
327                 // For compatibility reasons with old code.
328                 //
329                 public static void report_error (string error)
330                 {
331                         Console.Write ("ERROR: ");
332                         Console.WriteLine (error);
333                 }
334         }
335
336         public enum TimerType {
337                 FindMembers     = 0,
338                 TcFindMembers   = 1,
339                 MemberLookup    = 2,
340                 CachedLookup    = 3,
341                 CacheInit       = 4,
342                 MiscTimer       = 5,
343                 CountTimers     = 6
344         }
345
346         public enum CounterType {
347                 FindMembers     = 0,
348                 MemberCache     = 1,
349                 MiscCounter     = 2,
350                 CountCounters   = 3
351         }
352
353         public class Timer
354         {
355                 static DateTime[] timer_start;
356                 static TimeSpan[] timers;
357                 static long[] timer_counters;
358                 static long[] counters;
359
360                 static Timer ()
361                 {
362                         timer_start = new DateTime [(int) TimerType.CountTimers];
363                         timers = new TimeSpan [(int) TimerType.CountTimers];
364                         timer_counters = new long [(int) TimerType.CountTimers];
365                         counters = new long [(int) CounterType.CountCounters];
366
367                         for (int i = 0; i < (int) TimerType.CountTimers; i++) {
368                                 timer_start [i] = DateTime.Now;
369                                 timers [i] = TimeSpan.Zero;
370                         }
371                 }
372
373                 [Conditional("TIMER")]
374                 static public void IncrementCounter (CounterType which)
375                 {
376                         ++counters [(int) which];
377                 }
378
379                 [Conditional("TIMER")]
380                 static public void StartTimer (TimerType which)
381                 {
382                         timer_start [(int) which] = DateTime.Now;
383                 }
384
385                 [Conditional("TIMER")]
386                 static public void StopTimer (TimerType which)
387                 {
388                         timers [(int) which] += DateTime.Now - timer_start [(int) which];
389                         ++timer_counters [(int) which];
390                 }
391
392                 [Conditional("TIMER")]
393                 static public void ShowTimers ()
394                 {
395                         ShowTimer (TimerType.FindMembers, "- FindMembers timer");
396                         ShowTimer (TimerType.TcFindMembers, "- TypeContainer.FindMembers timer");
397                         ShowTimer (TimerType.MemberLookup, "- MemberLookup timer");
398                         ShowTimer (TimerType.CachedLookup, "- CachedLookup timer");
399                         ShowTimer (TimerType.CacheInit, "- Cache init");
400                         ShowTimer (TimerType.MiscTimer, "- Misc timer");
401
402                         ShowCounter (CounterType.FindMembers, "- Find members");
403                         ShowCounter (CounterType.MemberCache, "- Member cache");
404                         ShowCounter (CounterType.MiscCounter, "- Misc counter");
405                 }
406
407                 static public void ShowCounter (CounterType which, string msg)
408                 {
409                         Console.WriteLine ("{0} {1}", counters [(int) which], msg);
410                 }
411
412                 static public void ShowTimer (TimerType which, string msg)
413                 {
414                         Console.WriteLine (
415                                 "[{0:00}:{1:000}] {2} (used {3} times)",
416                                 (int) timers [(int) which].TotalSeconds,
417                                 timers [(int) which].Milliseconds, msg,
418                                 timer_counters [(int) which]);
419                 }
420         }
421
422         public class InternalErrorException : Exception {
423                 public InternalErrorException ()
424                         : base ("Internal error")
425                 {
426                 }
427
428                 public InternalErrorException (string message)
429                         : base (message)
430                 {
431                 }
432
433                 public InternalErrorException (string format, params object[] args)
434                         : this (String.Format (format, args))
435                 { }
436         }
437 }