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