Add testcase for multiple missing IDs
[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 // Copyright 2001 Ximian, Inc. (http://www.ximian.com)
8 //
9
10 using System;
11 using System.IO;
12 using System.Text;
13 using System.Collections.Generic;
14 using System.Diagnostics;
15 using System.Reflection;
16
17 namespace Mono.CSharp {
18
19         //
20         // Errors and warnings manager
21         //
22         public class Report
23         {
24                 /// <summary>  
25                 ///   Whether warnings should be considered errors
26                 /// </summary>
27                 public bool WarningsAreErrors;
28                 List<int> warnings_as_error;
29                 List<int> warnings_only;
30
31                 public static int DebugFlags = 0;
32
33                 public const int RuntimeErrorId = 10000;
34
35                 //
36                 // Keeps track of the warnings that we are ignoring
37                 //
38                 HashSet<int> warning_ignore_table;
39
40                 Dictionary<int, WarningRegions> warning_regions_table;
41
42                 int warning_level;
43
44                 ReportPrinter printer;
45
46                 int reporting_disabled;
47                 
48                 /// <summary>
49                 /// List of symbols related to reported error/warning. You have to fill it before error/warning is reported.
50                 /// </summary>
51                 List<string> extra_information = new List<string> ();
52
53                 // 
54                 // IF YOU ADD A NEW WARNING YOU HAVE TO ADD ITS ID HERE
55                 //
56                 public static readonly int[] AllWarnings = new int[] {
57                         28, 67, 78,
58                         105, 108, 109, 114, 162, 164, 168, 169, 183, 184, 197,
59                         219, 251, 252, 253, 278, 282,
60                         402, 414, 419, 420, 429, 436, 440, 458, 464, 465, 467, 469, 472,
61                         612, 618, 626, 628, 642, 649, 652, 658, 659, 660, 661, 665, 672, 675, 693,
62                         728,
63                         809,
64                         1030, 1058, 1066,
65                         1522, 1570, 1571, 1572, 1573, 1574, 1580, 1581, 1584, 1587, 1589, 1590, 1591, 1592,
66                         1616, 1633, 1634, 1635, 1685, 1690, 1691, 1692, 1695, 1696, 1699,
67                         1700, 1709, 1717, 1718, 1720,
68                         1901, 1981,
69                         2002, 2023, 2029,
70                         3000, 3001, 3002, 3003, 3005, 3006, 3007, 3008, 3009,
71                         3010, 3011, 3012, 3013, 3014, 3015, 3016, 3017, 3018, 3019,
72                         3021, 3022, 3023, 3024, 3026, 3027
73                 };
74
75                 static HashSet<int> AllWarningsHashSet;
76
77                 public Report (ReportPrinter printer)
78                 {
79                         if (printer == null)
80                                 throw new ArgumentNullException ("printer");
81
82                         this.printer = printer;
83                         warning_level = 4;
84                 }
85
86                 public void DisableReporting ()
87                 {
88                         ++reporting_disabled;
89                 }
90
91                 public void EnableReporting ()
92                 {
93                         --reporting_disabled;
94                 }
95
96                 public void FeatureIsNotAvailable (Location loc, string feature)
97                 {
98                         string version;
99                         switch (RootContext.Version) {
100                         case LanguageVersion.ISO_1:
101                                 version = "1.0";
102                                 break;
103                         case LanguageVersion.ISO_2:
104                                 version = "2.0";
105                                 break;
106                         case LanguageVersion.V_3:
107                                 version = "3.0";
108                                 break;
109                         default:
110                                 throw new InternalErrorException ("Invalid feature version", RootContext.Version);
111                         }
112
113                         Error (1644, loc,
114                                 "Feature `{0}' cannot be used because it is not part of the C# {1} language specification",
115                                       feature, version);
116                 }
117
118                 public void FeatureIsNotSupported (Location loc, string feature)
119                 {
120                         Error (1644, loc,
121                                 "Feature `{0}' is not supported in Mono mcs1 compiler. Consider using the `gmcs' compiler instead",
122                                 feature);
123                 }
124                 
125                 bool IsWarningEnabled (int code, int level, Location loc)
126                 {
127                         if (WarningLevel < level)
128                                 return false;
129
130                         if (IsWarningDisabledGlobally (code))
131                                 return false;
132
133                         if (warning_regions_table == null || loc.IsNull)
134                                 return true;
135
136                         WarningRegions regions;
137                         if (!warning_regions_table.TryGetValue (loc.File, out regions))
138                                 return true;
139
140                         return regions.IsWarningEnabled (code, loc.Row);
141                 }
142
143                 public bool IsWarningDisabledGlobally (int code)
144                 {
145                         return warning_ignore_table != null && warning_ignore_table.Contains (code);
146                 }
147
148                 bool IsWarningAsError (int code)
149                 {
150                         bool is_error = WarningsAreErrors;
151
152                         // Check specific list
153                         if (warnings_as_error != null)
154                                 is_error |= warnings_as_error.Contains (code);
155
156                         // Ignore excluded warnings
157                         if (warnings_only != null && warnings_only.Contains (code))
158                                 is_error = false;
159
160                         return is_error;
161                 }
162                         
163                 public void RuntimeMissingSupport (Location loc, string feature) 
164                 {
165                         Error (-88, loc, "Your .NET Runtime does not support `{0}'. Please use the latest Mono runtime instead.", feature);
166                 }
167
168                 /// <summary>
169                 /// In most error cases is very useful to have information about symbol that caused the error.
170                 /// Call this method before you call Report.Error when it makes sense.
171                 /// </summary>
172                 public void SymbolRelatedToPreviousError (Location loc, string symbol)
173                 {
174                         SymbolRelatedToPreviousError (loc.ToString (), symbol);
175                 }
176
177                 public void SymbolRelatedToPreviousError (MemberSpec ms)
178                 {
179                         if (reporting_disabled > 0 || !printer.HasRelatedSymbolSupport)
180                                 return;
181
182                         var mc = ms.MemberDefinition as MemberCore;
183                         while (ms is ElementTypeSpec) {
184                                 ms = ((ElementTypeSpec) ms).Element;
185                                 mc = ms.MemberDefinition as MemberCore;
186                         }
187
188                         if (mc != null) {
189                                 SymbolRelatedToPreviousError (mc);
190                         } else if (ms.MemberDefinition != null) {
191                                 SymbolRelatedToPreviousError (ms.MemberDefinition.Assembly.Location, "");
192                         }
193                 }
194
195                 public void SymbolRelatedToPreviousError (MemberCore mc)
196                 {
197                         SymbolRelatedToPreviousError (mc.Location, mc.GetSignatureForError ());
198                 }
199
200                 void SymbolRelatedToPreviousError (string loc, string symbol)
201                 {
202                         string msg = String.Format ("{0} (Location of the symbol related to previous ", loc);
203                         if (extra_information.Contains (msg))
204                                 return;
205
206                         extra_information.Add (msg);
207                 }
208
209                 public void AddWarningAsError (string warningId)
210                 {
211                         int id;
212                         try {
213                                 id = int.Parse (warningId);
214                         } catch {
215                                 CheckWarningCode (warningId, Location.Null);
216                                 return;
217                         }
218
219                         if (!CheckWarningCode (id, Location.Null))
220                                 return;
221
222                         if (warnings_as_error == null)
223                                 warnings_as_error = new List<int> ();
224                         
225                         warnings_as_error.Add (id);
226                 }
227
228                 public void RemoveWarningAsError (string warningId)
229                 {
230                         int id;
231                         try {
232                                 id = int.Parse (warningId);
233                         } catch {
234                                 CheckWarningCode (warningId, Location.Null);
235                                 return;
236                         }
237
238                         if (!CheckWarningCode (id, Location.Null))
239                                 return;
240
241                         if (warnings_only == null)
242                                 warnings_only = new List<int> ();
243
244                         warnings_only.Add (id);
245                 }
246
247                 public bool CheckWarningCode (string code, Location loc)
248                 {
249                         Warning (1691, 1, loc, "`{0}' is not a valid warning number", code);
250                         return false;
251                 }
252
253                 public bool CheckWarningCode (int code, Location loc)
254                 {
255                         if (AllWarningsHashSet == null)
256                                 AllWarningsHashSet = new HashSet<int> (AllWarnings);
257
258                         if (AllWarningsHashSet.Contains (code))
259                                 return true;
260
261                         return CheckWarningCode (code.ToString (), loc);
262                 }
263
264                 public void ExtraInformation (Location loc, string msg)
265                 {
266                         extra_information.Add (String.Format ("{0} {1}", loc, msg));
267                 }
268
269                 public WarningRegions RegisterWarningRegion (Location location)
270                 {
271                         WarningRegions regions;
272                         if (warning_regions_table == null) {
273                                 regions = null;
274                                 warning_regions_table = new Dictionary<int, WarningRegions> ();
275                         } else {
276                                 warning_regions_table.TryGetValue (location.File, out regions);
277                         }
278
279                         if (regions == null) {
280                                 regions = new WarningRegions ();
281                                 warning_regions_table.Add (location.File, regions);
282                         }
283
284                         return regions;
285                 }
286
287                 public void Warning (int code, int level, Location loc, string message)
288                 {
289                         if (reporting_disabled > 0)
290                                 return;
291
292                         if (!IsWarningEnabled (code, level, loc))
293                                 return;
294
295                         AbstractMessage msg;
296                         if (IsWarningAsError (code))
297                                 msg = new ErrorMessage (code, loc, message, extra_information);
298                         else
299                                 msg = new WarningMessage (code, loc, message, extra_information);
300
301                         extra_information.Clear ();
302                         printer.Print (msg);
303                 }
304
305                 public void Warning (int code, int level, Location loc, string format, string arg)
306                 {
307                         Warning (code, level, loc, String.Format (format, arg));
308                 }
309
310                 public void Warning (int code, int level, Location loc, string format, string arg1, string arg2)
311                 {
312                         Warning (code, level, loc, String.Format (format, arg1, arg2));
313                 }
314
315                 public void Warning (int code, int level, Location loc, string format, params object[] args)
316                 {
317                         Warning (code, level, loc, String.Format (format, args));
318                 }
319
320                 public void Warning (int code, int level, string message)
321                 {
322                         Warning (code, level, Location.Null, message);
323                 }
324
325                 public void Warning (int code, int level, string format, string arg)
326                 {
327                         Warning (code, level, Location.Null, format, arg);
328                 }
329
330                 public void Warning (int code, int level, string format, string arg1, string arg2)
331                 {
332                         Warning (code, level, Location.Null, format, arg1, arg2);
333                 }
334
335                 public void Warning (int code, int level, string format, params string[] args)
336                 {
337                         Warning (code, level, Location.Null, String.Format (format, args));
338                 }
339
340                 //
341                 // Warnings encountered so far
342                 //
343                 public int Warnings {
344                         get { return printer.WarningsCount; }
345                 }
346
347                 public void Error (int code, Location loc, string error)
348                 {
349                         if (reporting_disabled > 0)
350                                 return;
351
352                         ErrorMessage msg = new ErrorMessage (code, loc, error, extra_information);
353                         extra_information.Clear ();
354
355                         printer.Print (msg);
356                 }
357
358                 public void Error (int code, Location loc, string format, string arg)
359                 {
360                         Error (code, loc, String.Format (format, arg));
361                 }
362
363                 public void Error (int code, Location loc, string format, string arg1, string arg2)
364                 {
365                         Error (code, loc, String.Format (format, arg1, arg2));
366                 }
367
368                 public void Error (int code, Location loc, string format, params string[] args)
369                 {
370                         Error (code, loc, String.Format (format, args));
371                 }
372
373                 public void Error (int code, string error)
374                 {
375                         Error (code, Location.Null, error);
376                 }
377
378                 public void Error (int code, string format, string arg)
379                 {
380                         Error (code, Location.Null, format, arg);
381                 }
382
383                 public void Error (int code, string format, string arg1, string arg2)
384                 {
385                         Error (code, Location.Null, format, arg1, arg2);
386                 }
387
388                 public void Error (int code, string format, params string[] args)
389                 {
390                         Error (code, Location.Null, String.Format (format, args));
391                 }
392
393                 //
394                 // Errors encountered so far
395                 //
396                 public int Errors {
397                         get { return printer.ErrorsCount; }
398                 }
399
400                 public bool IsDisabled {
401                         get {
402                                 return reporting_disabled > 0;
403                         }
404                 }
405
406                 public ReportPrinter Printer {
407                         get { return printer; }
408                 }
409
410                 public void SetIgnoreWarning (int code)
411                 {
412                         if (warning_ignore_table == null)
413                                 warning_ignore_table = new HashSet<int> ();
414
415                         warning_ignore_table.Add (code);
416                 }
417
418                 public ReportPrinter SetPrinter (ReportPrinter printer)
419                 {
420                         ReportPrinter old = this.printer;
421                         this.printer = printer;
422                         return old;
423                 }
424
425                 public int WarningLevel {
426                         get {
427                                 return warning_level;
428                         }
429                         set {
430                                 warning_level = value;
431                         }
432                 }
433
434                 [Conditional ("MCS_DEBUG")]
435                 static public void Debug (string message, params object[] args)
436                 {
437                         Debug (4, message, args);
438                 }
439                         
440                 [Conditional ("MCS_DEBUG")]
441                 static public void Debug (int category, string message, params object[] args)
442                 {
443                         if ((category & DebugFlags) == 0)
444                                 return;
445
446                         StringBuilder sb = new StringBuilder (message);
447
448                         if ((args != null) && (args.Length > 0)) {
449                                 sb.Append (": ");
450
451                                 bool first = true;
452                                 foreach (object arg in args) {
453                                         if (first)
454                                                 first = false;
455                                         else
456                                                 sb.Append (", ");
457                                         if (arg == null)
458                                                 sb.Append ("null");
459 //                                      else if (arg is ICollection)
460 //                                              sb.Append (PrintCollection ((ICollection) arg));
461                                         else
462                                                 sb.Append (arg);
463                                 }
464                         }
465
466                         Console.WriteLine (sb.ToString ());
467                 }
468 /*
469                 static public string PrintCollection (ICollection collection)
470                 {
471                         StringBuilder sb = new StringBuilder ();
472
473                         sb.Append (collection.GetType ());
474                         sb.Append ("(");
475
476                         bool first = true;
477                         foreach (object o in collection) {
478                                 if (first)
479                                         first = false;
480                                 else
481                                         sb.Append (", ");
482                                 sb.Append (o);
483                         }
484
485                         sb.Append (")");
486                         return sb.ToString ();
487                 }
488 */ 
489         }
490
491         public abstract class AbstractMessage
492         {
493                 readonly string[] extra_info;
494                 protected readonly int code;
495                 protected readonly Location location;
496                 readonly string message;
497
498                 protected AbstractMessage (int code, Location loc, string msg, List<string> extraInfo)
499                 {
500                         this.code = code;
501                         if (code < 0)
502                                 this.code = 8000 - code;
503
504                         this.location = loc;
505                         this.message = msg;
506                         if (extraInfo.Count != 0) {
507                                 this.extra_info = extraInfo.ToArray ();
508                         }
509                 }
510
511                 protected AbstractMessage (AbstractMessage aMsg)
512                 {
513                         this.code = aMsg.code;
514                         this.location = aMsg.location;
515                         this.message = aMsg.message;
516                         this.extra_info = aMsg.extra_info;
517                 }
518
519                 public int Code {
520                         get { return code; }
521                 }
522
523                 public override bool Equals (object obj)
524                 {
525                         AbstractMessage msg = obj as AbstractMessage;
526                         if (msg == null)
527                                 return false;
528
529                         return code == msg.code && location.Equals (msg.location) && message == msg.message;
530                 }
531
532                 public override int GetHashCode ()
533                 {
534                         return code.GetHashCode ();
535                 }
536
537                 public abstract bool IsWarning { get; }
538
539                 public Location Location {
540                         get { return location; }
541                 }
542
543                 public abstract string MessageType { get; }
544
545                 public string[] RelatedSymbols {
546                         get { return extra_info; }
547                 }
548
549                 public string Text {
550                         get { return message; }
551                 }
552         }
553
554         sealed class WarningMessage : AbstractMessage
555         {
556                 public WarningMessage (int code, Location loc, string message, List<string> extra_info)
557                         : base (code, loc, message, extra_info)
558                 {
559                 }
560
561                 public override bool IsWarning {
562                         get { return true; }
563                 }
564
565                 public override string MessageType {
566                         get {
567                                 return "warning";
568                         }
569                 }
570         }
571
572         sealed class ErrorMessage : AbstractMessage
573         {
574                 public ErrorMessage (int code, Location loc, string message, List<string> extraInfo)
575                         : base (code, loc, message, extraInfo)
576                 {
577                 }
578
579                 public ErrorMessage (AbstractMessage aMsg)
580                         : base (aMsg)
581                 {
582                 }
583
584                 public override bool IsWarning {
585                         get { return false; }
586                 }
587
588                 public override string MessageType {
589                         get {
590                                 return "error";
591                         }
592                 }
593         }
594
595         //
596         // Generic base for any message writer
597         //
598         public abstract class ReportPrinter {
599                 /// <summary>  
600                 ///   Whether to dump a stack trace on errors. 
601                 /// </summary>
602                 public bool Stacktrace;
603                 
604                 int warnings, errors;
605
606                 public int WarningsCount {
607                         get { return warnings; }
608                 }
609                 
610                 public int ErrorsCount {
611                         get { return errors; }
612                 }
613
614                 protected virtual string FormatText (string txt)
615                 {
616                         return txt;
617                 }
618
619                 //
620                 // When (symbols related to previous ...) can be used
621                 //
622                 public virtual bool HasRelatedSymbolSupport {
623                         get { return true; }
624                 }
625
626                 public virtual void Print (AbstractMessage msg)
627                 {
628                         if (msg.IsWarning)
629                                 ++warnings;
630                         else
631                                 ++errors;
632                 }
633
634                 protected void Print (AbstractMessage msg, TextWriter output)
635                 {
636                         StringBuilder txt = new StringBuilder ();
637                         if (!msg.Location.IsNull) {
638                                 txt.Append (msg.Location.ToString ());
639                                 txt.Append (" ");
640                         }
641
642                         txt.AppendFormat ("{0} CS{1:0000}: {2}", msg.MessageType, msg.Code, msg.Text);
643
644                         if (!msg.IsWarning)
645                                 output.WriteLine (FormatText (txt.ToString ()));
646                         else
647                                 output.WriteLine (txt.ToString ());
648
649                         if (msg.RelatedSymbols != null) {
650                                 foreach (string s in msg.RelatedSymbols)
651                                         output.WriteLine (s + msg.MessageType + ")");
652                         }
653                 }
654         }
655
656         //
657         // Default message recorder, it uses two types of message groups.
658         // Common messages: messages reported in all sessions.
659         // Merged messages: union of all messages in all sessions. 
660         //
661         // Used by the Lambda expressions to compile the code with various
662         // parameter values, or by attribute resolver
663         //
664         class SessionReportPrinter : ReportPrinter
665         {
666                 List<AbstractMessage> session_messages;
667                 //
668                 // A collection of exactly same messages reported in all sessions
669                 //
670                 List<AbstractMessage> common_messages;
671
672                 //
673                 // A collection of unique messages reported in all sessions
674                 //
675                 List<AbstractMessage> merged_messages;
676
677                 public override void Print (AbstractMessage msg)
678                 {
679                         //
680                         // This line is useful when debugging recorded messages
681                         //
682                         // Console.WriteLine ("RECORDING: {0}", msg.ToString ());
683
684                         if (session_messages == null)
685                                 session_messages = new List<AbstractMessage> ();
686
687                         session_messages.Add (msg);
688
689                         base.Print (msg);
690                 }
691
692                 public void EndSession ()
693                 {
694                         if (session_messages == null)
695                                 return;
696
697                         //
698                         // Handles the first session
699                         //
700                         if (common_messages == null) {
701                                 common_messages = new List<AbstractMessage> (session_messages);
702                                 merged_messages = session_messages;
703                                 session_messages = null;
704                                 return;
705                         }
706
707                         //
708                         // Store common messages if any
709                         //
710                         for (int i = 0; i < common_messages.Count; ++i) {
711                                 AbstractMessage cmsg = common_messages[i];
712                                 bool common_msg_found = false;
713                                 foreach (AbstractMessage msg in session_messages) {
714                                         if (cmsg.Equals (msg)) {
715                                                 common_msg_found = true;
716                                                 break;
717                                         }
718                                 }
719
720                                 if (!common_msg_found)
721                                         common_messages.RemoveAt (i);
722                         }
723
724                         //
725                         // Merge session and previous messages
726                         //
727                         for (int i = 0; i < session_messages.Count; ++i) {
728                                 AbstractMessage msg = session_messages[i];
729                                 bool msg_found = false;
730                                 for (int ii = 0; ii < merged_messages.Count; ++ii) {
731                                         if (msg.Equals (merged_messages[ii])) {
732                                                 msg_found = true;
733                                                 break;
734                                         }
735                                 }
736
737                                 if (!msg_found)
738                                         merged_messages.Add (msg);
739                         }
740                 }
741
742                 public bool IsEmpty {
743                         get {
744                                 return merged_messages == null && common_messages == null;
745                         }
746                 }
747
748                 //
749                 // Prints collected messages, common messages have a priority
750                 //
751                 public bool Merge (ReportPrinter dest)
752                 {
753                         var messages_to_print = merged_messages;
754                         if (common_messages != null && common_messages.Count > 0) {
755                                 messages_to_print = common_messages;
756                         }
757
758                         if (messages_to_print == null)
759                                 return false;
760
761                         bool error_msg = false;
762                         foreach (AbstractMessage msg in messages_to_print) {
763                                 dest.Print (msg);
764                                 error_msg |= !msg.IsWarning;
765                         }
766
767                         return error_msg;
768                 }
769         }
770
771         class StreamReportPrinter : ReportPrinter
772         {
773                 readonly TextWriter writer;
774
775                 public StreamReportPrinter (TextWriter writer)
776                 {
777                         this.writer = writer;
778                 }
779
780                 public override void Print (AbstractMessage msg)
781                 {
782                         Print (msg, writer);
783                         base.Print (msg);
784                 }
785         }
786
787         class ConsoleReportPrinter : StreamReportPrinter
788         {
789                 static readonly string prefix, postfix;
790
791                 static ConsoleReportPrinter ()
792                 {
793                         string term = Environment.GetEnvironmentVariable ("TERM");
794                         bool xterm_colors = false;
795                         
796                         switch (term){
797                         case "xterm":
798                         case "rxvt":
799                         case "rxvt-unicode": 
800                                 if (Environment.GetEnvironmentVariable ("COLORTERM") != null){
801                                         xterm_colors = true;
802                                 }
803                                 break;
804
805                         case "xterm-color":
806                                 xterm_colors = true;
807                                 break;
808                         }
809                         if (!xterm_colors)
810                                 return;
811
812                         if (!(UnixUtils.isatty (1) && UnixUtils.isatty (2)))
813                                 return;
814                         
815                         string config = Environment.GetEnvironmentVariable ("MCS_COLORS");
816                         if (config == null){
817                                 config = "errors=red";
818                                 //config = "brightwhite,red";
819                         }
820
821                         if (config == "disable")
822                                 return;
823
824                         if (!config.StartsWith ("errors="))
825                                 return;
826
827                         config = config.Substring (7);
828                         
829                         int p = config.IndexOf (",");
830                         if (p == -1)
831                                 prefix = GetForeground (config);
832                         else
833                                 prefix = GetBackground (config.Substring (p+1)) + GetForeground (config.Substring (0, p));
834                         postfix = "\x001b[0m";
835                 }
836
837                 public ConsoleReportPrinter ()
838                         : base (Console.Error)
839                 {
840                 }
841
842                 public ConsoleReportPrinter (TextWriter writer)
843                         : base (writer)
844                 {
845                 }
846
847                 public int Fatal { get; set; }
848
849                 static int NameToCode (string s)
850                 {
851                         switch (s) {
852                         case "black":
853                                 return 0;
854                         case "red":
855                                 return 1;
856                         case "green":
857                                 return 2;
858                         case "yellow":
859                                 return 3;
860                         case "blue":
861                                 return 4;
862                         case "magenta":
863                                 return 5;
864                         case "cyan":
865                                 return 6;
866                         case "grey":
867                         case "white":
868                                 return 7;
869                         }
870                         return 7;
871                 }
872
873                 //
874                 // maps a color name to its xterm color code
875                 //
876                 static string GetForeground (string s)
877                 {
878                         string highcode;
879
880                         if (s.StartsWith ("bright")) {
881                                 highcode = "1;";
882                                 s = s.Substring (6);
883                         } else
884                                 highcode = "";
885
886                         return "\x001b[" + highcode + (30 + NameToCode (s)).ToString () + "m";
887                 }
888
889                 static string GetBackground (string s)
890                 {
891                         return "\x001b[" + (40 + NameToCode (s)).ToString () + "m";
892                 }
893
894                 protected override string FormatText (string txt)
895                 {
896                         if (prefix != null)
897                                 return prefix + txt + postfix;
898
899                         return txt;
900                 }
901
902                 static string FriendlyStackTrace (StackTrace t)
903                 {               
904                         StringBuilder sb = new StringBuilder ();
905                         
906                         bool foundUserCode = false;
907                         
908                         for (int i = 0; i < t.FrameCount; i++) {
909                                 StackFrame f = t.GetFrame (i);
910                                 MethodBase mb = f.GetMethod ();
911                                 
912                                 if (!foundUserCode && mb.ReflectedType == typeof (Report))
913                                         continue;
914                                 
915                                 foundUserCode = true;
916                                 
917                                 sb.Append ("\tin ");
918                                 
919                                 if (f.GetFileLineNumber () > 0)
920                                         sb.AppendFormat ("(at {0}:{1}) ", f.GetFileName (), f.GetFileLineNumber ());
921                                 
922                                 sb.AppendFormat ("{0}.{1} (", mb.ReflectedType.Name, mb.Name);
923                                 
924                                 bool first = true;
925                                 foreach (ParameterInfo pi in mb.GetParameters ()) {
926                                         if (!first)
927                                                 sb.Append (", ");
928                                         first = false;
929
930                                         sb.Append (pi.ParameterType.FullName);
931                                 }
932                                 sb.Append (")\n");
933                         }
934         
935                         return sb.ToString ();
936                 }
937
938                 int print_count;
939                 public override void Print (AbstractMessage msg)
940                 {
941                         base.Print (msg);
942
943                         if (Stacktrace)
944                                 Console.WriteLine (FriendlyStackTrace (new StackTrace (true)));
945
946                         if (++print_count == Fatal)
947                                 throw new Exception (msg.Text);
948                 }
949
950                 public static string FriendlyStackTrace (Exception e)
951                 {
952                         return FriendlyStackTrace (new StackTrace (e, true));
953                 }
954
955                 public static void StackTrace ()
956                 {
957                         Console.WriteLine (FriendlyStackTrace (new StackTrace (true)));
958                 }
959         }
960
961         public enum TimerType {
962                 FindMembers     = 0,
963                 TcFindMembers   = 1,
964                 MemberLookup    = 2,
965                 CachedLookup    = 3,
966                 CacheInit       = 4,
967                 MiscTimer       = 5,
968                 CountTimers     = 6
969         }
970
971         public enum CounterType {
972                 FindMembers     = 0,
973                 MemberCache     = 1,
974                 MiscCounter     = 2,
975                 CountCounters   = 3
976         }
977
978         public class Timer
979         {
980                 static DateTime[] timer_start;
981                 static TimeSpan[] timers;
982                 static long[] timer_counters;
983                 static long[] counters;
984
985                 static Timer ()
986                 {
987                         timer_start = new DateTime [(int) TimerType.CountTimers];
988                         timers = new TimeSpan [(int) TimerType.CountTimers];
989                         timer_counters = new long [(int) TimerType.CountTimers];
990                         counters = new long [(int) CounterType.CountCounters];
991
992                         for (int i = 0; i < (int) TimerType.CountTimers; i++) {
993                                 timer_start [i] = DateTime.Now;
994                                 timers [i] = TimeSpan.Zero;
995                         }
996                 }
997
998                 [Conditional("TIMER")]
999                 static public void IncrementCounter (CounterType which)
1000                 {
1001                         ++counters [(int) which];
1002                 }
1003
1004                 [Conditional("TIMER")]
1005                 static public void StartTimer (TimerType which)
1006                 {
1007                         timer_start [(int) which] = DateTime.Now;
1008                 }
1009
1010                 [Conditional("TIMER")]
1011                 static public void StopTimer (TimerType which)
1012                 {
1013                         timers [(int) which] += DateTime.Now - timer_start [(int) which];
1014                         ++timer_counters [(int) which];
1015                 }
1016
1017                 [Conditional("TIMER")]
1018                 static public void ShowTimers ()
1019                 {
1020                         ShowTimer (TimerType.FindMembers, "- FindMembers timer");
1021                         ShowTimer (TimerType.TcFindMembers, "- TypeContainer.FindMembers timer");
1022                         ShowTimer (TimerType.MemberLookup, "- MemberLookup timer");
1023                         ShowTimer (TimerType.CachedLookup, "- CachedLookup timer");
1024                         ShowTimer (TimerType.CacheInit, "- Cache init");
1025                         ShowTimer (TimerType.MiscTimer, "- Misc timer");
1026
1027                         ShowCounter (CounterType.FindMembers, "- Find members");
1028                         ShowCounter (CounterType.MemberCache, "- Member cache");
1029                         ShowCounter (CounterType.MiscCounter, "- Misc counter");
1030                 }
1031
1032                 static public void ShowCounter (CounterType which, string msg)
1033                 {
1034                         Console.WriteLine ("{0} {1}", counters [(int) which], msg);
1035                 }
1036
1037                 static public void ShowTimer (TimerType which, string msg)
1038                 {
1039                         Console.WriteLine (
1040                                 "[{0:00}:{1:000}] {2} (used {3} times)",
1041                                 (int) timers [(int) which].TotalSeconds,
1042                                 timers [(int) which].Milliseconds, msg,
1043                                 timer_counters [(int) which]);
1044                 }
1045         }
1046
1047         public class InternalErrorException : Exception {
1048                 public InternalErrorException (MemberCore mc, Exception e)
1049                         : base (mc.Location + " " + mc.GetSignatureForError (), e)
1050                 {
1051                 }
1052
1053                 public InternalErrorException ()
1054                         : base ("Internal error")
1055                 {
1056                 }
1057
1058                 public InternalErrorException (string message)
1059                         : base (message)
1060                 {
1061                 }
1062
1063                 public InternalErrorException (string message, params object[] args)
1064                         : base (String.Format (message, args))
1065                 {
1066                 }
1067
1068                 public InternalErrorException (Exception exception, string message, params object[] args)
1069                         : base (String.Format (message, args), exception)
1070                 {
1071                 }
1072                 
1073                 public InternalErrorException (Exception e, Location loc)
1074                         : base (loc.ToString (), e)
1075                 {
1076                 }
1077         }
1078
1079         /// <summary>
1080         /// Handles #pragma warning
1081         /// </summary>
1082         public class WarningRegions {
1083
1084                 abstract class PragmaCmd
1085                 {
1086                         public int Line;
1087
1088                         protected PragmaCmd (int line)
1089                         {
1090                                 Line = line;
1091                         }
1092
1093                         public abstract bool IsEnabled (int code, bool previous);
1094                 }
1095                 
1096                 class Disable : PragmaCmd
1097                 {
1098                         int code;
1099                         public Disable (int line, int code)
1100                                 : base (line)
1101                         {
1102                                 this.code = code;
1103                         }
1104
1105                         public override bool IsEnabled (int code, bool previous)
1106                         {
1107                                 return this.code == code ? false : previous;
1108                         }
1109                 }
1110
1111                 class DisableAll : PragmaCmd
1112                 {
1113                         public DisableAll (int line)
1114                                 : base (line) {}
1115
1116                         public override bool IsEnabled(int code, bool previous)
1117                         {
1118                                 return false;
1119                         }
1120                 }
1121
1122                 class Enable : PragmaCmd
1123                 {
1124                         int code;
1125                         public Enable (int line, int code)
1126                                 : base (line)
1127                         {
1128                                 this.code = code;
1129                         }
1130
1131                         public override bool IsEnabled(int code, bool previous)
1132                         {
1133                                 return this.code == code ? true : previous;
1134                         }
1135                 }
1136
1137                 class EnableAll : PragmaCmd
1138                 {
1139                         public EnableAll (int line)
1140                                 : base (line) {}
1141
1142                         public override bool IsEnabled(int code, bool previous)
1143                         {
1144                                 return true;
1145                         }
1146                 }
1147
1148
1149                 List<PragmaCmd> regions = new List<PragmaCmd> ();
1150
1151                 public void WarningDisable (int line)
1152                 {
1153                         regions.Add (new DisableAll (line));
1154                 }
1155
1156                 public void WarningDisable (Location location, int code, Report Report)
1157                 {
1158                         if (Report.CheckWarningCode (code, location))
1159                                 regions.Add (new Disable (location.Row, code));
1160                 }
1161
1162                 public void WarningEnable (int line)
1163                 {
1164                         regions.Add (new EnableAll (line));
1165                 }
1166
1167                 public void WarningEnable (Location location, int code, Report Report)
1168                 {
1169                         if (!Report.CheckWarningCode (code, location))
1170                                 return;
1171
1172                         if (Report.IsWarningDisabledGlobally (code))
1173                                 Report.Warning (1635, 1, location, "Cannot restore warning `CS{0:0000}' because it was disabled globally", code);
1174
1175                         regions.Add (new Enable (location.Row, code));
1176                 }
1177
1178                 public bool IsWarningEnabled (int code, int src_line)
1179                 {
1180                         bool result = true;
1181                         foreach (PragmaCmd pragma in regions) {
1182                                 if (src_line < pragma.Line)
1183                                         break;
1184
1185                                 result = pragma.IsEnabled (code, result);
1186                         }
1187                         return result;
1188                 }
1189         }
1190 }