* ComboBox.cs: Remove some debug code that was blinding me.
[mono.git] / mcs / gmcs / report.cs
1 //
2 // report.cs: report errors and warnings.
3 //
4 // Author: Miguel de Icaza (miguel@ximian.com)
5 //         Marek Safar (marek.safar@seznam.cz)         
6 //
7 // (C) 2001 Ximian, Inc. (http://www.ximian.com)
8 //
9
10 using System;
11 using System.IO;
12 using System.Text;
13 using System.Collections;
14 using System.Collections.Specialized;
15 using System.Diagnostics;
16 using System.Reflection;
17 using System.Reflection.Emit;
18
19 namespace Mono.CSharp {
20
21         /// <summary>
22         ///   This class is used to report errors and warnings t te user.
23         /// </summary>
24         public class Report {
25                 /// <summary>  
26                 ///   Errors encountered so far
27                 /// </summary>
28                 static public int Errors;
29
30                 /// <summary>  
31                 ///   Warnings encountered so far
32                 /// </summary>
33                 static public int Warnings;
34
35                 /// <summary>  
36                 ///   Whether errors should be throw an exception
37                 /// </summary>
38                 static public bool Fatal;
39                 
40                 /// <summary>  
41                 ///   Whether warnings should be considered errors
42                 /// </summary>
43                 static public bool WarningsAreErrors;
44
45                 /// <summary>  
46                 ///   Whether to dump a stack trace on errors. 
47                 /// </summary>
48                 static public bool Stacktrace;
49
50                 static public TextWriter Stderr = Console.Error;
51                 
52                 //
53                 // If the 'expected' error code is reported then the
54                 // compilation succeeds.
55                 //
56                 // Used for the test suite to excercise the error codes
57                 //
58                 static int expected_error = 0;
59
60                 //
61                 // Keeps track of the warnings that we are ignoring
62                 //
63                 public static Hashtable warning_ignore_table;
64
65                 static Hashtable warning_regions_table;
66
67                 /// <summary>
68                 /// List of symbols related to reported error/warning. You have to fill it before error/warning is reported.
69                 /// </summary>
70                 static StringCollection extra_information = new StringCollection ();
71
72                 // 
73                 // IF YOU ADD A NEW WARNING YOU HAVE TO ADD ITS ID HERE
74                 //
75                 public static readonly int[] AllWarnings = new int[] {
76                         28, 67, 78,
77                         105, 108, 109, 114, 162, 164, 168, 169, 183, 184, 197,
78                         219, 251, 252, 253, 282,
79                         419, 420, 429, 436, 440, 465, 469,
80                         612, 618, 626, 628, 642, 649, 652, 658, 659, 660, 661, 665, 672,
81                         1030, 1058,
82                         1522, 1570, 1571, 1572, 1573, 1574, 1580, 1581, 1584, 1587, 1589, 1590, 1591, 1592,
83                         1616, 1633, 1634, 1635, 1690, 1691, 1692,
84                         1717, 1718,
85                         1901,
86                         2002, 2023,
87                         3005, 3012, 3019, 3021, 3022, 3023, 3026, 3027,
88                         // gmcs warnings start here
89                         414, 1700
90                 };
91
92                 static Report ()
93                 {
94                         // Just to be sure that binary search is working
95                         Array.Sort (AllWarnings);
96                 }
97
98                 public static void Reset ()
99                 {
100                         Errors = Warnings = 0;
101                         WarningsAreErrors = false;
102                         warning_ignore_table = null;
103                         warning_regions_table = null;
104                 }
105
106                 abstract class AbstractMessage {
107
108                         static void Check (int code)
109                         {
110                                 if (code == expected_error) {
111                                         Environment.Exit (0);
112                                 }
113                         }
114
115                         public abstract bool IsWarning { get; }
116
117                         public abstract string MessageType { get; }
118
119                         public virtual void Print (int code, string location, string text)
120                         {
121                                 if (code < 0)
122                                         code = 8000-code;
123
124                                 StringBuilder msg = new StringBuilder ();
125                                 if (location.Length != 0) {
126                                         msg.Append (location);
127                                         msg.Append (' ');
128                                 }
129                                 msg.AppendFormat ("{0} CS{1:0000}: {2}", MessageType, code, text);
130                                 Stderr.WriteLine (msg.ToString ());
131
132                                 foreach (string s in extra_information) 
133                                         Stderr.WriteLine (s + MessageType);
134
135                                 extra_information.Clear ();
136
137                                 if (Stacktrace)
138                                         Console.WriteLine (FriendlyStackTrace (new StackTrace (true)));
139
140                                 if (Fatal) {
141                                         if (!IsWarning || WarningsAreErrors)
142                                                 throw new Exception (text);
143                                 }
144
145                                 Check (code);
146                         }
147
148                         public virtual void Print (int code, Location location, string text)
149                         {
150                                 Print (code, location.IsNull ? "" : location.ToString (), text);
151                         }
152                 }
153
154                 sealed class WarningMessage : AbstractMessage {
155                         Location loc = Location.Null;
156                         readonly int Level;
157
158                         public WarningMessage (int level)
159                         {
160                                 Level = level;
161                         }
162
163                         public override bool IsWarning {
164                                 get { return true; }
165                         }
166
167                         bool IsEnabled (int code)
168                         {
169                                 if (RootContext.WarningLevel < Level)
170                                         return false;
171
172                                 if (warning_ignore_table != null) {
173                                         if (warning_ignore_table.Contains (code)) {
174                                                 return false;
175                                         }
176                                 }
177
178                                 if (warning_regions_table == null || loc.Equals (Location.Null))
179                                         return true;
180
181                                 WarningRegions regions = (WarningRegions)warning_regions_table [loc.Name];
182                                 if (regions == null)
183                                         return true;
184
185                                 return regions.IsWarningEnabled (code, loc.Row);
186                         }
187
188                         public override void Print(int code, string location, string text)
189                         {
190                                 if (!IsEnabled (code)) {
191                                         extra_information.Clear ();
192                                         return;
193                                 }
194
195                                 if (WarningsAreErrors) {
196                                         new ErrorMessage ().Print (code, location, text);
197                                         return;
198                                 }
199
200                                 Warnings++;
201                                 base.Print (code, location, text);
202                         }
203
204                         public override void Print(int code, Location location, string text)
205                         {
206                                 loc = location;
207                                 base.Print (code, location, text);
208                         }
209
210                         public override string MessageType {
211                                 get {
212                                         return "warning";
213                                 }
214                         }
215                 }
216
217                 sealed class ErrorMessage : AbstractMessage {
218
219                         public override void Print(int code, string location, string text)
220                         {
221                                 Errors++;
222                                 base.Print (code, location, text);
223                         }
224
225                         public override bool IsWarning {
226                                 get { return false; }
227                         }
228
229                         public override string MessageType {
230                                 get {
231                                         return "error";
232                                 }
233                         }
234
235                 }
236
237                 public static void FeatureIsNotStandardized (Location loc, string feature)
238                 {
239                         Report.Error (1644, loc, "Feature `{0}' cannot be used because it is not part of the standardized ISO C# language specification", feature);
240                 }
241                 
242                 public static string FriendlyStackTrace (Exception e)
243                 {
244                         return FriendlyStackTrace (new StackTrace (e, true));
245                 }
246                 
247                 static string FriendlyStackTrace (StackTrace t)
248                 {               
249                         StringBuilder sb = new StringBuilder ();
250                         
251                         bool foundUserCode = false;
252                         
253                         for (int i = 0; i < t.FrameCount; i++) {
254                                 StackFrame f = t.GetFrame (i);
255                                 MethodBase mb = f.GetMethod ();
256                                 
257                                 if (!foundUserCode && mb.ReflectedType == typeof (Report))
258                                         continue;
259                                 
260                                 foundUserCode = true;
261                                 
262                                 sb.Append ("\tin ");
263                                 
264                                 if (f.GetFileLineNumber () > 0)
265                                         sb.AppendFormat ("(at {0}:{1}) ", f.GetFileName (), f.GetFileLineNumber ());
266                                 
267                                 sb.AppendFormat ("{0}.{1} (", mb.ReflectedType.Name, mb.Name);
268                                 
269                                 bool first = true;
270                                 foreach (ParameterInfo pi in mb.GetParameters ()) {
271                                         if (!first)
272                                                 sb.Append (", ");
273                                         first = false;
274                                         
275                                         sb.Append (TypeManager.CSharpName (pi.ParameterType));
276                                 }
277                                 sb.Append (")\n");
278                         }
279         
280                         return sb.ToString ();
281                 }
282
283                 public static void StackTrace ()
284                 {
285                         Console.WriteLine (FriendlyStackTrace (new StackTrace (true)));
286                 }
287
288                 public static bool IsValidWarning (int code)
289                 {       
290                         return Array.BinarySearch (AllWarnings, code) >= 0;
291                 }
292                         
293                 static public void RuntimeMissingSupport (Location loc, string feature) 
294                 {
295                         Report.Error (-88, loc, "Your .NET Runtime does not support `{0}'. Please use the latest Mono runtime instead.", feature);
296                 }
297
298                 /// <summary>
299                 /// In most error cases is very useful to have information about symbol that caused the error.
300                 /// Call this method before you call Report.Error when it makes sense.
301                 /// </summary>
302                 static public void SymbolRelatedToPreviousError (Location loc, string symbol)
303                 {
304                         SymbolRelatedToPreviousError (loc.ToString (), symbol);
305                 }
306
307                 static public void SymbolRelatedToPreviousError (MemberInfo mi)
308                 {
309                         Type dt = TypeManager.DropGenericTypeArguments (mi.DeclaringType);
310                         TypeContainer temp_ds = TypeManager.LookupGenericTypeContainer (dt);
311                         if (temp_ds == null) {
312                                 SymbolRelatedToPreviousError (dt.Assembly.Location, TypeManager.GetFullNameSignature (mi));
313                         } else {
314                                 MethodBase mb = mi as MethodBase;
315                                 if (mb != null) {
316                                         mb = TypeManager.DropGenericMethodArguments (mb);
317                                         IMethodData md = TypeManager.GetMethod (mb);
318                                         SymbolRelatedToPreviousError (md.Location, md.GetSignatureForError ());
319                                         return;
320                                 }
321
322                                 MemberCore mc = temp_ds.GetDefinition (mi.Name);
323                                 SymbolRelatedToPreviousError (mc);
324                         }
325                 }
326
327                 static public void SymbolRelatedToPreviousError (MemberCore mc)
328                 {
329                         SymbolRelatedToPreviousError (mc.Location, mc.GetSignatureForError ());
330                 }
331
332                 static public void SymbolRelatedToPreviousError (Type type)
333                 {
334                         type = TypeManager.DropGenericTypeArguments (type);
335
336                         if (type.IsGenericParameter) {
337                                 TypeParameter tp = TypeManager.LookupTypeParameter (type);
338                                 if (tp != null) {
339                                         SymbolRelatedToPreviousError (tp.Location, "");
340                                         return;
341                                 }
342                         }
343
344                         if (type is TypeBuilder) {
345                                 DeclSpace temp_ds = TypeManager.LookupDeclSpace (type);
346                                 SymbolRelatedToPreviousError (temp_ds.Location, TypeManager.CSharpName (type));
347                         } else if (type.HasElementType) {
348                                 SymbolRelatedToPreviousError (type.GetElementType ());
349                         } else {
350                                 SymbolRelatedToPreviousError (type.Assembly.Location, TypeManager.CSharpName (type));
351                         }
352                 }
353
354                 static void SymbolRelatedToPreviousError (string loc, string symbol)
355                 {
356                         extra_information.Add (String.Format ("{0}: `{1}', name of symbol related to previous ", loc, symbol));
357                 }
358
359                 public static void ExtraInformation (Location loc, string msg)
360                 {
361                         extra_information.Add (String.Format ("{0} {1}", loc, msg));
362                 }
363
364                 public static WarningRegions RegisterWarningRegion (Location location)
365                 {
366                         if (warning_regions_table == null)
367                                 warning_regions_table = new Hashtable ();
368
369                         WarningRegions regions = (WarningRegions)warning_regions_table [location.Name];
370                         if (regions == null) {
371                                 regions = new WarningRegions ();
372                                 warning_regions_table.Add (location.Name, regions);
373                         }
374                         return regions;
375                 }
376
377                 static public void Warning (int code, int level, Location loc, string message)
378                 {
379                         WarningMessage w = new WarningMessage (level);
380                         w.Print (code, loc, message);
381                 }
382
383                 static public void Warning (int code, int level, Location loc, string format, string arg)
384                 {
385                         WarningMessage w = new WarningMessage (level);
386                         w.Print (code, loc, String.Format (format, arg));
387                 }
388
389                 static public void Warning (int code, int level, Location loc, string format, string arg1, string arg2)
390                 {
391                         WarningMessage w = new WarningMessage (level);
392                         w.Print (code, loc, String.Format (format, arg1, arg2));
393                 }
394
395                 static public void Warning (int code, int level, Location loc, string format, params string[] args)
396                 {
397                         WarningMessage w = new WarningMessage (level);
398                         w.Print (code, loc, String.Format (format, args));
399                 }
400
401                 static public void Warning (int code, int level, string message)
402                 {
403                         Warning (code, level, Location.Null, message);
404                 }
405
406                 static public void Warning (int code, int level, string format, string arg)
407                 {
408                         Warning (code, level, Location.Null, format, arg);
409                 }
410
411                 static public void Warning (int code, int level, string format, string arg1, string arg2)
412                 {
413                         Warning (code, level, Location.Null, format, arg1, arg2);
414                 }
415
416                 static public void Warning (int code, int level, string format, params string[] args)
417                 {
418                         Warning (code, level, Location.Null, String.Format (format, args));
419                 }
420
421                 static public void Error (int code, Location loc, string error)
422                 {
423                         new ErrorMessage ().Print (code, loc, error);
424                 }
425
426                 static public void Error (int code, Location loc, string format, string arg)
427                 {
428                         new ErrorMessage ().Print (code, loc, String.Format (format, arg));
429                 }
430
431                 static public void Error (int code, Location loc, string format, string arg1, string arg2)
432                 {
433                         new ErrorMessage ().Print (code, loc, String.Format (format, arg1, arg2));
434                 }
435
436                 static public void Error (int code, Location loc, string format, params string[] args)
437                 {
438                         Error (code, loc, String.Format (format, args));
439                 }
440
441                 static public void Error (int code, string error)
442                 {
443                         Error (code, Location.Null, error);
444                 }
445
446                 static public void Error (int code, string format, string arg)
447                 {
448                         Error (code, Location.Null, format, arg);
449                 }
450
451                 static public void Error (int code, string format, string arg1, string arg2)
452                 {
453                         Error (code, Location.Null, format, arg1, arg2);
454                 }
455
456                 static public void Error (int code, string format, params string[] args)
457                 {
458                         Error (code, Location.Null, String.Format (format, args));
459                 }
460
461                 static public void SetIgnoreWarning (int code)
462                 {
463                         if (warning_ignore_table == null)
464                                 warning_ignore_table = new Hashtable ();
465
466                         warning_ignore_table [code] = true;
467                 }
468                 
469                 static public int ExpectedError {
470                         set {
471                                 expected_error = value;
472                         }
473                         get {
474                                 return expected_error;
475                         }
476                 }
477
478                 public static int DebugFlags = 0;
479
480                 [Conditional ("MCS_DEBUG")]
481                 static public void Debug (string message, params object[] args)
482                 {
483                         Debug (4, message, args);
484                 }
485                         
486                 [Conditional ("MCS_DEBUG")]
487                 static public void Debug (int category, string message, params object[] args)
488                 {
489                         if ((category & DebugFlags) == 0)
490                                 return;
491
492                         StringBuilder sb = new StringBuilder (message);
493
494                         if ((args != null) && (args.Length > 0)) {
495                                 sb.Append (": ");
496
497                                 bool first = true;
498                                 foreach (object arg in args) {
499                                         if (first)
500                                                 first = false;
501                                         else
502                                                 sb.Append (", ");
503                                         if (arg == null)
504                                                 sb.Append ("null");
505                                         else if (arg is ICollection)
506                                                 sb.Append (PrintCollection ((ICollection) arg));
507                                         else
508                                                 sb.Append (arg);
509                                 }
510                         }
511
512                         Console.WriteLine (sb.ToString ());
513                 }
514
515                 static public string PrintCollection (ICollection collection)
516                 {
517                         StringBuilder sb = new StringBuilder ();
518
519                         sb.Append (collection.GetType ());
520                         sb.Append ("(");
521
522                         bool first = true;
523                         foreach (object o in collection) {
524                                 if (first)
525                                         first = false;
526                                 else
527                                         sb.Append (", ");
528                                 sb.Append (o);
529                         }
530
531                         sb.Append (")");
532                         return sb.ToString ();
533                 }
534         }
535
536         public enum TimerType {
537                 FindMembers     = 0,
538                 TcFindMembers   = 1,
539                 MemberLookup    = 2,
540                 CachedLookup    = 3,
541                 CacheInit       = 4,
542                 MiscTimer       = 5,
543                 CountTimers     = 6
544         }
545
546         public enum CounterType {
547                 FindMembers     = 0,
548                 MemberCache     = 1,
549                 MiscCounter     = 2,
550                 CountCounters   = 3
551         }
552
553         public class Timer
554         {
555                 static DateTime[] timer_start;
556                 static TimeSpan[] timers;
557                 static long[] timer_counters;
558                 static long[] counters;
559
560                 static Timer ()
561                 {
562                         timer_start = new DateTime [(int) TimerType.CountTimers];
563                         timers = new TimeSpan [(int) TimerType.CountTimers];
564                         timer_counters = new long [(int) TimerType.CountTimers];
565                         counters = new long [(int) CounterType.CountCounters];
566
567                         for (int i = 0; i < (int) TimerType.CountTimers; i++) {
568                                 timer_start [i] = DateTime.Now;
569                                 timers [i] = TimeSpan.Zero;
570                         }
571                 }
572
573                 [Conditional("TIMER")]
574                 static public void IncrementCounter (CounterType which)
575                 {
576                         ++counters [(int) which];
577                 }
578
579                 [Conditional("TIMER")]
580                 static public void StartTimer (TimerType which)
581                 {
582                         timer_start [(int) which] = DateTime.Now;
583                 }
584
585                 [Conditional("TIMER")]
586                 static public void StopTimer (TimerType which)
587                 {
588                         timers [(int) which] += DateTime.Now - timer_start [(int) which];
589                         ++timer_counters [(int) which];
590                 }
591
592                 [Conditional("TIMER")]
593                 static public void ShowTimers ()
594                 {
595                         ShowTimer (TimerType.FindMembers, "- FindMembers timer");
596                         ShowTimer (TimerType.TcFindMembers, "- TypeContainer.FindMembers timer");
597                         ShowTimer (TimerType.MemberLookup, "- MemberLookup timer");
598                         ShowTimer (TimerType.CachedLookup, "- CachedLookup timer");
599                         ShowTimer (TimerType.CacheInit, "- Cache init");
600                         ShowTimer (TimerType.MiscTimer, "- Misc timer");
601
602                         ShowCounter (CounterType.FindMembers, "- Find members");
603                         ShowCounter (CounterType.MemberCache, "- Member cache");
604                         ShowCounter (CounterType.MiscCounter, "- Misc counter");
605                 }
606
607                 static public void ShowCounter (CounterType which, string msg)
608                 {
609                         Console.WriteLine ("{0} {1}", counters [(int) which], msg);
610                 }
611
612                 static public void ShowTimer (TimerType which, string msg)
613                 {
614                         Console.WriteLine (
615                                 "[{0:00}:{1:000}] {2} (used {3} times)",
616                                 (int) timers [(int) which].TotalSeconds,
617                                 timers [(int) which].Milliseconds, msg,
618                                 timer_counters [(int) which]);
619                 }
620         }
621
622         public class InternalErrorException : Exception {
623                 public InternalErrorException (Location loc, string text, Exception e)
624                         : base (loc + " " + text, e)
625                 {
626                 }
627
628                 public InternalErrorException ()
629                         : base ("Internal error")
630                 {
631                 }
632
633                 public InternalErrorException (string message)
634                         : base (message)
635                 {
636                 }
637         }
638
639         /// <summary>
640         /// Handles #pragma warning
641         /// </summary>
642         public class WarningRegions {
643
644                 abstract class PragmaCmd
645                 {
646                         public int Line;
647
648                         protected PragmaCmd (int line)
649                         {
650                                 Line = line;
651                         }
652
653                         public abstract bool IsEnabled (int code, bool previous);
654                 }
655                 
656                 class Disable : PragmaCmd
657                 {
658                         int code;
659                         public Disable (int line, int code)
660                                 : base (line)
661                         {
662                                 this.code = code;
663                         }
664
665                         public override bool IsEnabled (int code, bool previous)
666                         {
667                                 return this.code == code ? false : previous;
668                         }
669                 }
670
671                 class DisableAll : PragmaCmd
672                 {
673                         public DisableAll (int line)
674                                 : base (line) {}
675
676                         public override bool IsEnabled(int code, bool previous)
677                         {
678                                 return false;
679                         }
680                 }
681
682                 class Enable : PragmaCmd
683                 {
684                         int code;
685                         public Enable (int line, int code)
686                                 : base (line)
687                         {
688                                 this.code = code;
689                         }
690
691                         public override bool IsEnabled(int code, bool previous)
692                         {
693                                 return this.code == code ? true : previous;
694                         }
695                 }
696
697                 class EnableAll : PragmaCmd
698                 {
699                         public EnableAll (int line)
700                                 : base (line) {}
701
702                         public override bool IsEnabled(int code, bool previous)
703                         {
704                                 return true;
705                         }
706                 }
707
708
709                 ArrayList regions = new ArrayList ();
710
711                 public void WarningDisable (int line)
712                 {
713                         regions.Add (new DisableAll (line));
714                 }
715
716                 public void WarningDisable (Location location, int code)
717                 {
718                         if (CheckWarningCode (code, location))
719                                 regions.Add (new Disable (location.Row, code));
720                 }
721
722                 public void WarningEnable (int line)
723                 {
724                         regions.Add (new EnableAll (line));
725                 }
726
727                 public void WarningEnable (Location location, int code)
728                 {
729                         if (CheckWarningCode (code, location))
730                                 regions.Add (new Enable (location.Row, code));
731                 }
732
733                 public bool IsWarningEnabled (int code, int src_line)
734                 {
735                         bool result = true;
736                         foreach (PragmaCmd pragma in regions) {
737                                 if (src_line < pragma.Line)
738                                         break;
739
740                                 result = pragma.IsEnabled (code, result);
741                         }
742                         return result;
743                 }
744
745                 static bool CheckWarningCode (int code, Location loc)
746                 {
747                         if (Report.IsValidWarning (code))
748                                 return true;
749
750                         Report.Warning (1691, 1, loc, "`{0}' is not a valid warning number", code.ToString ());
751                         return false;
752                 }
753         }
754 }