8f5c0c7d9fc13d9cb4af215dd12e5573e0abcf68
[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                 struct WarningData {
63                         public WarningData (int level, string text) {
64                                 Level = level;
65                                 Message = text;
66                         }
67
68                         public bool IsEnabled ()
69                         {
70                                 return RootContext.WarningLevel >= Level;
71                         }
72
73                         public string Format (params object[] args)
74                         {
75                                 return String.Format (Message, args);
76                         }
77
78                         readonly string Message;
79                         readonly int Level;
80                 }
81
82                 static string GetErrorMsg (int error_no)
83                 {
84                         switch (error_no) {
85                                 case 3001: return "Argument type '{0}' is not CLS-compliant";
86                                 case 3002: return "Return type of '{0}' is not CLS-compliant";
87                                 case 3003: return "Type of '{0}' is not CLS-compliant";
88                                 case 3005: return "Identifier '{0}' differing only in case is not CLS-compliant";
89                                 case 3006: return "Overloaded method '{0}' differing only in ref or out, or in array rank, is not CLS-compliant";
90                                 case 3008: return "Identifier '{0}' is not CLS-compliant";
91                                 case 3009: return "'{0}': base type '{1}' is not CLS-compliant";
92                                 case 3010: return "'{0}': CLS-compliant interfaces must have only CLS-compliant members";
93                                 case 3011: return "'{0}': only CLS-compliant members can be abstract";
94                                 case 3013: return "Added modules must be marked with the CLSCompliant attribute to match the assembly";
95                                 case 3014: return "'{0}' cannot be marked as CLS-compliant because the assembly does not have a CLSCompliant attribute";
96                                 case 3015: return "'{0}' has no accessible constructors which use only CLS-compliant types";
97                                 case 3016: return "Arrays as attribute arguments are not CLS-compliant";
98                         }
99                         throw new InternalErrorException (String.Format ("Missing error '{0}' text", error_no));
100                 }
101
102                 static WarningData GetWarningMsg (int warn_no)
103                 {
104                         switch (warn_no) {
105                                 case 3012: return new WarningData (1, "You must specify the CLSCompliant attribute on the assembly, not the module, to enable CLS compliance checking");
106                         }
107
108                         throw new InternalErrorException (String.Format ("Wrong warning number '{0}'", warn_no));
109                 }
110                 
111                 static void Check (int code)
112                 {
113                         if (code == expected_error){
114                                 if (Fatal)
115                                         throw new Exception ();
116                                 
117                                 Environment.Exit (0);
118                         }
119                 }
120                 
121                 public static string FriendlyStackTrace (Exception e)
122                 {
123                         return FriendlyStackTrace (new StackTrace (e, true));
124                 }
125                 
126                 static string FriendlyStackTrace (StackTrace t)
127                 {               
128                         StringBuilder sb = new StringBuilder ();
129                         
130                         bool foundUserCode = false;
131                         
132                         for (int i = 0; i < t.FrameCount; i++) {
133                                 StackFrame f = t.GetFrame (i);
134                                 MethodBase mb = f.GetMethod ();
135                                 
136                                 if (!foundUserCode && mb.ReflectedType == typeof (Report))
137                                         continue;
138                                 
139                                 foundUserCode = true;
140                                 
141                                 sb.Append ("\tin ");
142                                 
143                                 if (f.GetFileLineNumber () > 0)
144                                         sb.AppendFormat ("(at {0}:{1}) ", f.GetFileName (), f.GetFileLineNumber ());
145                                 
146                                 sb.AppendFormat ("{0}.{1} (", mb.ReflectedType.Name, mb.Name);
147                                 
148                                 bool first = true;
149                                 foreach (ParameterInfo pi in mb.GetParameters ()) {
150                                         if (!first)
151                                                 sb.Append (", ");
152                                         first = false;
153                                         
154                                         sb.Append (TypeManager.CSharpName (pi.ParameterType));
155                                 }
156                                 sb.Append (")\n");
157                         }
158         
159                         return sb.ToString ();
160                 }
161                 
162                 static public void LocationOfPreviousError (Location loc) {
163                         Console.WriteLine (String.Format ("{0}({1}) (Location of symbol related to previous error)", loc.Name, loc.Row));
164                 }
165
166                 static public void RealError (string msg)
167                 {
168                         Errors++;
169                         Console.WriteLine (msg);
170
171                         if (Stacktrace)
172                                 Console.WriteLine (FriendlyStackTrace (new StackTrace (true)));
173                         
174                         if (Fatal)
175                                 throw new Exception (msg);
176                 }
177
178
179                 /// <summary>
180                 /// Method reports warning message. Only one reason why exist Warning and Report methods is beter code readability.
181                 /// </summary>
182                 static public void Warning (int code, Location loc, params object[] args)
183                 {
184                         WarningData warning = GetWarningMsg (code);
185                         if (!warning.IsEnabled ())
186                                 return;
187
188                         Warning (code, loc, warning.Format (args));
189                 }
190
191                 /// <summary>
192                 /// Reports error message.
193                 /// </summary>
194                 static public void Error_T (int code, Location loc, params object[] args)
195                 {
196                         Error_T (code, String.Format ("{0}({1})", loc.Name, loc.Row), args);
197                 }
198
199                 static public void Error_T (int code, string location, params object[] args)
200                 {
201                         string errorText = String.Format (GetErrorMsg (code), args);
202                         PrintError (code, location, errorText);
203                 }
204
205                 static void PrintError (int code, string l, string text)
206                 {
207                         if (code < 0)
208                                 code = 8000-code;
209                         
210                         string msg = String.Format ("{0} error CS{1:0000}: {2}", l, code, text);
211                         RealError (msg);
212                         Check (code);
213                 }
214
215                 static public void Error (int code, Location l, string text)
216                 {
217                         if (code < 0)
218                                 code = 8000-code;
219                         
220                         string msg = String.Format (
221                                 "{0}({1}) error CS{2:0000}: {3}", l.Name, l.Row, code, text);
222 //                              "{0}({1}) error CS{2}: {3}", l.Name, l.Row, code, text);
223                         
224                         RealError (msg);
225                         Check (code);
226                 }
227
228                 static public void Warning (int code, Location l, string text)
229                 {
230                         if (code < 0)
231                                 code = 8000-code;
232                         
233                         if (warning_ignore_table != null){
234                                 if (warning_ignore_table.Contains (code))
235                                         return;
236                         }
237                         
238                         if (WarningsAreErrors)
239                                 Error (code, l, text);
240                         else {
241                                 string row;
242                                 
243                                 if (Location.IsNull (l))
244                                         row = "";
245                                 else
246                                         row = l.Row.ToString ();
247                                 
248                                 Console.WriteLine (String.Format (
249                                         "{0}({1}) warning CS{2:0000}: {3}",
250 //                                      "{0}({1}) warning CS{2}: {3}",
251                                         l.Name,  row, code, text));
252                                 Warnings++;
253                                 Check (code);
254
255                                 if (Stacktrace)
256                                         Console.WriteLine (new StackTrace ().ToString ());
257                         }
258                 }
259                 
260                 static public void Warning (int code, string text)
261                 {
262                         Warning (code, Location.Null, text);
263                 }
264
265                 static public void Warning (int code, int level, string text)
266                 {
267                         if (RootContext.WarningLevel >= level)
268                                 Warning (code, Location.Null, text);
269                 }
270
271                 static public void Warning (int code, int level, Location l, string text)
272                 {
273                         if (RootContext.WarningLevel >= level)
274                                 Warning (code, l, text);
275                 }
276
277                 static public void Error (int code, string text)
278                 {
279                         if (code < 0)
280                                 code = 8000-code;
281                         
282                         string msg = String.Format ("error CS{0:0000}: {1}", code, text);
283 //                      string msg = String.Format ("error CS{0}: {1}", code, text);
284                         
285                         RealError (msg);
286                         Check (code);
287                 }
288
289                 static public void Error (int code, Location loc, string format, params object[] args)
290                 {
291                         Error (code, loc, String.Format (format, args));
292                 }
293
294                 static public void Warning (int code, Location loc, string format, params object[] args)
295                 {
296                         Warning (code, loc, String.Format (format, args));
297                 }
298
299                 static public void Warning (int code, string format, params object[] args)
300                 {
301                         Warning (code, String.Format (format, args));
302                 }
303
304                 static public void Message (Message m)
305                 {
306                         if (m is ErrorMessage)
307                                 Error (m.code, m.text);
308                         else
309                                 Warning (m.code, m.text);
310                 }
311
312                 static public void SetIgnoreWarning (int code)
313                 {
314                         if (warning_ignore_table == null)
315                                 warning_ignore_table = new Hashtable ();
316
317                         warning_ignore_table [code] = true;
318                 }
319                 
320                 static public int ExpectedError {
321                         set {
322                                 expected_error = value;
323                         }
324                         get {
325                                 return expected_error;
326                         }
327                 }
328
329                 public static int DebugFlags = 0;
330
331                 [Conditional ("MCS_DEBUG")]
332                 static public void Debug (string message, params object[] args)
333                 {
334                         Debug (4, message, args);
335                 }
336                         
337                 [Conditional ("MCS_DEBUG")]
338                 static public void Debug (int category, string message, params object[] args)
339                 {
340                         if ((category & DebugFlags) == 0)
341                                 return;
342
343                         StringBuilder sb = new StringBuilder (message);
344
345                         if ((args != null) && (args.Length > 0)) {
346                                 sb.Append (": ");
347
348                                 bool first = true;
349                                 foreach (object arg in args) {
350                                         if (first)
351                                                 first = false;
352                                         else
353                                                 sb.Append (", ");
354                                         if (arg == null)
355                                                 sb.Append ("null");
356                                         else if (arg is ICollection)
357                                                 sb.Append (PrintCollection ((ICollection) arg));
358                                         else if (arg is IntPtr)
359                                                 sb.Append (String.Format ("IntPtr(0x{0:x})", ((IntPtr) arg).ToInt32 ()));
360                                         else
361                                                 sb.Append (arg);
362                                 }
363                         }
364
365                         Console.WriteLine (sb.ToString ());
366                 }
367
368                 static public string PrintCollection (ICollection collection)
369                 {
370                         StringBuilder sb = new StringBuilder ();
371
372                         sb.Append (collection.GetType ());
373                         sb.Append ("(");
374
375                         bool first = true;
376                         foreach (object o in collection) {
377                                 if (first)
378                                         first = false;
379                                 else
380                                         sb.Append (", ");
381                                 sb.Append (o);
382                         }
383
384                         sb.Append (")");
385                         return sb.ToString ();
386                 }
387         }
388
389         public class Message {
390                 public int code;
391                 public string text;
392                 
393                 public Message (int code, string text)
394                 {
395                         this.code = code;
396                         this.text = text;
397                 }
398         }
399
400         public class WarningMessage : Message {
401                 public WarningMessage (int code, string text) : base (code, text)
402                 {
403                 }
404         }
405
406         public class ErrorMessage : Message {
407                 public ErrorMessage (int code, string text) : base (code, text)
408                 {
409                 }
410
411                 //
412                 // For compatibility reasons with old code.
413                 //
414                 public static void report_error (string error)
415                 {
416                         Console.Write ("ERROR: ");
417                         Console.WriteLine (error);
418                 }
419         }
420
421         public enum TimerType {
422                 FindMembers     = 0,
423                 TcFindMembers   = 1,
424                 MemberLookup    = 2,
425                 CachedLookup    = 3,
426                 CacheInit       = 4,
427                 MiscTimer       = 5,
428                 CountTimers     = 6
429         }
430
431         public enum CounterType {
432                 FindMembers     = 0,
433                 MemberCache     = 1,
434                 MiscCounter     = 2,
435                 CountCounters   = 3
436         }
437
438         public class Timer
439         {
440                 static DateTime[] timer_start;
441                 static TimeSpan[] timers;
442                 static long[] timer_counters;
443                 static long[] counters;
444
445                 static Timer ()
446                 {
447                         timer_start = new DateTime [(int) TimerType.CountTimers];
448                         timers = new TimeSpan [(int) TimerType.CountTimers];
449                         timer_counters = new long [(int) TimerType.CountTimers];
450                         counters = new long [(int) CounterType.CountCounters];
451
452                         for (int i = 0; i < (int) TimerType.CountTimers; i++) {
453                                 timer_start [i] = DateTime.Now;
454                                 timers [i] = TimeSpan.Zero;
455                         }
456                 }
457
458                 [Conditional("TIMER")]
459                 static public void IncrementCounter (CounterType which)
460                 {
461                         ++counters [(int) which];
462                 }
463
464                 [Conditional("TIMER")]
465                 static public void StartTimer (TimerType which)
466                 {
467                         timer_start [(int) which] = DateTime.Now;
468                 }
469
470                 [Conditional("TIMER")]
471                 static public void StopTimer (TimerType which)
472                 {
473                         timers [(int) which] += DateTime.Now - timer_start [(int) which];
474                         ++timer_counters [(int) which];
475                 }
476
477                 [Conditional("TIMER")]
478                 static public void ShowTimers ()
479                 {
480                         ShowTimer (TimerType.FindMembers, "- FindMembers timer");
481                         ShowTimer (TimerType.TcFindMembers, "- TypeContainer.FindMembers timer");
482                         ShowTimer (TimerType.MemberLookup, "- MemberLookup timer");
483                         ShowTimer (TimerType.CachedLookup, "- CachedLookup timer");
484                         ShowTimer (TimerType.CacheInit, "- Cache init");
485                         ShowTimer (TimerType.MiscTimer, "- Misc timer");
486
487                         ShowCounter (CounterType.FindMembers, "- Find members");
488                         ShowCounter (CounterType.MemberCache, "- Member cache");
489                         ShowCounter (CounterType.MiscCounter, "- Misc counter");
490                 }
491
492                 static public void ShowCounter (CounterType which, string msg)
493                 {
494                         Console.WriteLine ("{0} {1}", counters [(int) which], msg);
495                 }
496
497                 static public void ShowTimer (TimerType which, string msg)
498                 {
499                         Console.WriteLine (
500                                 "[{0:00}:{1:000}] {2} (used {3} times)",
501                                 (int) timers [(int) which].TotalSeconds,
502                                 timers [(int) which].Milliseconds, msg,
503                                 timer_counters [(int) which]);
504                 }
505         }
506
507         public class InternalErrorException : Exception {
508                 public InternalErrorException ()
509                         : base ("Internal error")
510                 {
511                 }
512
513                 public InternalErrorException (string message)
514                         : base (message)
515                 {
516                 }
517
518                 public InternalErrorException (string format, params object[] args)
519                         : this (String.Format (format, args))
520                 { }
521         }
522 }