2008-11-05 Francisco Figueiredo Jr. <francisco@npgsql.org>
[mono.git] / mcs / mcs / report.cs
index 3b4278739cdd3961f941685a9f24db71b220995f..8122aa13dfb75603ceb91e5c4abbe3815455dcd0 100644 (file)
@@ -4,7 +4,7 @@
 // Author: Miguel de Icaza (miguel@ximian.com)
 //         Marek Safar (marek.safar@seznam.cz)         
 //
-// (C) 2001 Ximian, Inc. (http://www.ximian.com)
+// Copyright 2001 Ximian, Inc. (http://www.ximian.com)
 //
 
 using System;
@@ -99,9 +99,13 @@ namespace Mono.CSharp {
                        1717, 1718, 1720,
                        1901,
                        2002, 2023, 2029,
-                       3005, 3012, 3018, 3019, 3021, 3022, 3023, 3026, 3027,
+                       3000, 3001, 3002, 3003, 3005, 3006, 3007, 3008, 3009,
+                       3010, 3011, 3012, 3013, 3014, 3015, 3016, 3017, 3018, 3019,
+                       3021, 3022, 3023, 3026, 3027,
+                       
+                       414,    // Non ISO-1 warnings
 #if GMCS_SOURCE
-                       402, 414, 693, 1058, 1700, 3024
+                       402, 458, 464, 693, 1058, 1700, 3024
 #endif
                };
 
@@ -118,6 +122,7 @@ namespace Mono.CSharp {
                        warning_ignore_table = null;
                        warning_regions_table = null;
                        reporting_disabled = false;
+                       error_stack = warning_stack = null;
                }
 
                public static void DisableReporting ()
@@ -137,8 +142,10 @@ namespace Mono.CSharp {
 
                public static void EnableReporting ()
                {
-                       if (warning_stack != null)
+                       if (warning_stack != null && warning_stack.Count > 0)
                                Warnings = (int) warning_stack.Pop ();
+                       else
+                               Warnings = 0;
 
                        Errors = (int) error_stack.Pop ();
                        if (error_stack.Count == 0) {
@@ -157,6 +164,7 @@ namespace Mono.CSharp {
 
                public interface IMessageRecorder
                {
+                       bool IsEmpty { get; }
                        void EndSession ();
                        void AddMessage (AbstractMessage msg);
                        bool PrintMessages ();
@@ -238,6 +246,12 @@ namespace Mono.CSharp {
                                session_messages.Add (msg);
                        }
 
+                       public bool IsEmpty {
+                               get {
+                                       return merged_messages == null && common_messages == null;
+                               }
+                       }
+
                        //
                        // Prints collected messages, common messages have a priority
                        //
@@ -331,7 +345,13 @@ namespace Mono.CSharp {
                                        msg.Append (" ");
                                }
                                msg.AppendFormat ("{0} CS{1:0000}: {2}", MessageType, code, message);
-                               Stderr.WriteLine (msg.ToString ());
+
+                               //
+                               // 
+                               if (Stderr == Console.Error)
+                                       Stderr.WriteLine (ColorFormat (msg.ToString ()));
+                               else
+                                       Stderr.WriteLine (msg.ToString ());
 
                                if (extra_info != null) {
                                        foreach (string s in extra_info)
@@ -348,6 +368,11 @@ namespace Mono.CSharp {
 
                                Check (code);
                        }
+
+                       protected virtual string ColorFormat (string s)
+                       {
+                               return s;
+                       }
                }
 
                sealed class WarningMessage : AbstractMessage
@@ -406,8 +431,101 @@ namespace Mono.CSharp {
                        }
                }
 
+               static int NameToCode (string s)
+               {
+                       switch (s){
+                       case "black":
+                               return 0;
+                       case "red":
+                               return 1;
+                       case "green":
+                               return 2;
+                       case "yellow":
+                               return 3;
+                       case "blue":
+                               return 4;
+                       case "magenta":
+                               return 5;
+                       case "cyan":
+                               return 6;
+                       case "grey":
+                       case "white":
+                               return 7;
+                       }
+                       return 7;
+               }
+               
+               //
+               // maps a color name to its xterm color code
+               //
+               static string GetForeground (string s)
+               {
+                       string highcode;
+
+                       if (s.StartsWith ("bright")){
+                               highcode = "1;";
+                               s = s.Substring (6);
+                       } else
+                               highcode = "";
+
+                       return "\x001b[" + highcode + (30 + NameToCode (s)).ToString () + "m";
+               }
+
+               static string GetBackground (string s)
+               {
+                       return "\x001b[" + (40 + NameToCode (s)).ToString () + "m";
+               }
+               
                sealed class ErrorMessage : AbstractMessage
                {
+                       static string prefix, postfix;
+
+                       static ErrorMessage ()
+                       {
+                               string term = Environment.GetEnvironmentVariable ("TERM");
+                               bool xterm_colors = false;
+                               
+                               switch (term){
+                               case "xterm":
+                               case "rxvt":
+                               case "rxvt-unicode": 
+                                       if (Environment.GetEnvironmentVariable ("COLORTERM") != null){
+                                               xterm_colors = true;
+                                       }
+                                       break;
+
+                               case "xterm-color":
+                                       xterm_colors = true;
+                                       break;
+                               }
+                               if (!xterm_colors)
+                                       return;
+
+                               if (!(UnixUtils.isatty (1) && UnixUtils.isatty (2)))
+                                       return;
+                               
+                               string config = Environment.GetEnvironmentVariable ("MCS_COLORS");
+                               if (config == null){
+                                       config = "errors=red";
+                                       //config = "brightwhite,red";
+                               }
+
+                               if (config == "disable")
+                                       return;
+
+                               if (!config.StartsWith ("errors="))
+                                       return;
+
+                               config = config.Substring (7);
+                               
+                               int p = config.IndexOf (",");
+                               if (p == -1)
+                                       prefix = GetForeground (config);
+                               else
+                                       prefix = GetBackground (config.Substring (p+1)) + GetForeground (config.Substring (0, p));
+                               postfix = "\x001b[0m";
+                       }
+
                        public ErrorMessage (int code, Location loc, string message, ArrayList extraInfo)
                                : base (code, loc, message, extraInfo)
                        {
@@ -418,6 +536,13 @@ namespace Mono.CSharp {
                        {
                        }
 
+                       protected override string ColorFormat (string s)
+                       {
+                               if (prefix != null)
+                                       return prefix + s + postfix;
+                               return s;
+                       }
+                       
                        public override void Print()
                        {
                                Errors++;
@@ -446,7 +571,7 @@ namespace Mono.CSharp {
                                version = "2.0";
                                break;
                        case LanguageVersion.Default_MCS:
-                               Report.Error (1644, loc, "Feature `{0}' is not available in Mono mcs compiler. Consider using Mono gmcs compiler instead",
+                               Report.Error (1644, loc, "Feature `{0}' is not available in Mono mcs1 compiler. Consider using the `gmcs' compiler instead",
                                              feature);
                                return;
                        default:
@@ -546,8 +671,10 @@ namespace Mono.CSharp {
                                        return;
                                }
 
+                               // FIXME: Completely wrong, it has to use FindMembers
                                MemberCore mc = temp_ds.GetDefinition (mi.Name);
-                               SymbolRelatedToPreviousError (mc);
+                               if (mc != null)
+                                       SymbolRelatedToPreviousError (mc);
                        }
                }
 
@@ -574,7 +701,7 @@ namespace Mono.CSharp {
                        if (type is TypeBuilder) {
                                DeclSpace temp_ds = TypeManager.LookupDeclSpace (type);
                                SymbolRelatedToPreviousError (temp_ds.Location, TypeManager.CSharpName (type));
-                       } else if (type.HasElementType) {
+                       } else if (TypeManager.HasElementType (type)) {
                                SymbolRelatedToPreviousError (type.GetElementType ());
                        } else {
                                SymbolRelatedToPreviousError (type.Assembly.Location, TypeManager.CSharpName (type));
@@ -583,7 +710,11 @@ namespace Mono.CSharp {
 
                static void SymbolRelatedToPreviousError (string loc, string symbol)
                {
-                       extra_information.Add (String.Format ("{0} (Location of the symbol related to previous ", loc));
+                       string msg = String.Format ("{0} (Location of the symbol related to previous ", loc);
+                       if (extra_information.Contains (msg))
+                               return;
+
+                       extra_information.Add (msg);
                }
 
                public static void ExtraInformation (Location loc, string msg)