2007-12-27 Marek Safar <marek.safar@gmail.com>
[mono.git] / mcs / mcs / report.cs
index c479fbe3a260c559ab7f6db972ea3f28d4fad62e..8bf9b1f7d3168f5991b405430b457ca70b63568d 100644 (file)
@@ -101,7 +101,7 @@ namespace Mono.CSharp {
                        2002, 2023, 2029,
                        3005, 3012, 3018, 3019, 3021, 3022, 3023, 3026, 3027,
 #if GMCS_SOURCE
-                       402, 414, 693, 1058, 1700, 3024
+                       402, 414, 458, 693, 1058, 1700, 3024
 #endif
                };
 
@@ -331,7 +331,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 +354,11 @@ namespace Mono.CSharp {
 
                                Check (code);
                        }
+
+                       public virtual string ColorFormat (string s)
+                       {
+                               return s;
+                       }
                }
 
                sealed class WarningMessage : AbstractMessage
@@ -406,8 +417,98 @@ 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;
+                               }
+                               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);
+                               
+                               if (!xterm_colors)
+                                       return;
+
+                               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 +519,13 @@ namespace Mono.CSharp {
                        {
                        }
 
+                       public override string ColorFormat (string s)
+                       {
+                               if (prefix != null)
+                                       return prefix + s + postfix;
+                               return s;
+                       }
+                       
                        public override void Print()
                        {
                                Errors++;
@@ -438,12 +546,20 @@ namespace Mono.CSharp {
                public static void FeatureIsNotAvailable (Location loc, string feature)
                {
                        string version;
-                       if (RootContext.Version == LanguageVersion.ISO_1)
+                       switch (RootContext.Version) {
+                       case LanguageVersion.ISO_1:
                                version = "1.0";
-                       else if (RootContext.Version == LanguageVersion.ISO_2)
+                               break;
+                       case LanguageVersion.ISO_2:
                                version = "2.0";
-                       else
+                               break;
+                       case LanguageVersion.Default_MCS:
+                               Report.Error (1644, loc, "Feature `{0}' is not available in Mono mcs compiler. Consider using Mono gmcs compiler instead",
+                                             feature);
+                               return;
+                       default:
                                throw new InternalErrorException ("Invalid feature version", RootContext.Version);
+                       }
 
                        Report.Error (1644, loc,
                                "Feature `{0}' cannot be used because it is not part of the C# {1} language specification",
@@ -521,6 +637,11 @@ namespace Mono.CSharp {
                                return;
 
                        Type dt = TypeManager.DropGenericTypeArguments (mi.DeclaringType);
+                       if (TypeManager.IsDelegateType (dt)) {
+                               SymbolRelatedToPreviousError (dt);
+                               return;
+                       }                       
+                       
                        DeclSpace temp_ds = TypeManager.LookupDeclSpace (dt);
                        if (temp_ds == null) {
                                SymbolRelatedToPreviousError (dt.Assembly.Location, TypeManager.GetFullNameSignature (mi));