X-Git-Url: http://wien.tomnetworks.com/gitweb/?a=blobdiff_plain;f=mcs%2Fmcs%2Freport.cs;h=8bf9b1f7d3168f5991b405430b457ca70b63568d;hb=4c2991cb66f1739fa1a51e40f2620fa2fef35728;hp=393547762c165e452afccd9cdd4173a82333d39d;hpb=c32ba0d619a7974bcba33552db9faf56c4f04083;p=mono.git diff --git a/mcs/mcs/report.cs b/mcs/mcs/report.cs index 393547762c1..8bf9b1f7d31 100644 --- a/mcs/mcs/report.cs +++ b/mcs/mcs/report.cs @@ -42,6 +42,7 @@ namespace Mono.CSharp { /// static public bool WarningsAreErrors; + /// /// Whether to dump a stack trace on errors. /// @@ -64,10 +65,23 @@ namespace Mono.CSharp { static Hashtable warning_regions_table; + // + // This is used to save/restore the error state. When the + // error stack contains elements, warnings and errors are not + // reported to the user. This is used by the Lambda expression + // support to compile the code with various parameter values. + // A stack because of `Report.Errors == errors;' + // + static Stack error_stack; + static Stack warning_stack; + static bool reporting_disabled; + + static int warning_level; + /// /// List of symbols related to reported error/warning. You have to fill it before error/warning is reported. /// - static StringCollection extra_information = new StringCollection (); + static ArrayList extra_information = new ArrayList (); // // IF YOU ADD A NEW WARNING YOU HAVE TO ADD ITS ID HERE @@ -75,18 +89,19 @@ namespace Mono.CSharp { public static readonly int[] AllWarnings = new int[] { 28, 67, 78, 105, 108, 109, 114, 162, 164, 168, 169, 183, 184, 197, - 219, 251, 252, 253, 282, - 419, 420, 429, 436, 440, 465, 467, 469, - 612, 618, 626, 628, 642, 649, 652, 658, 659, 660, 661, 665, 672, - 1030, 1058, + 219, 251, 252, 253, 278, 282, + 419, 420, 429, 436, 440, 465, 467, 469, 472, + 612, 618, 626, 628, 642, 649, 652, 658, 659, 660, 661, 665, 672, 675, + 809, + 1030, 1522, 1570, 1571, 1572, 1573, 1574, 1580, 1581, 1584, 1587, 1589, 1590, 1591, 1592, - 1616, 1633, 1634, 1635, 1690, 1691, 1692, + 1616, 1633, 1634, 1635, 1685, 1690, 1691, 1692, 1717, 1718, 1720, 1901, - 2002, 2023, - 3005, 3012, 3019, 3021, 3022, 3023, 3026, 3027, + 2002, 2023, 2029, + 3005, 3012, 3018, 3019, 3021, 3022, 3023, 3026, 3027, #if GMCS_SOURCE - 402, 414, 693, 1700, 3024 + 402, 414, 458, 693, 1058, 1700, 3024 #endif }; @@ -102,9 +117,174 @@ namespace Mono.CSharp { WarningsAreErrors = false; warning_ignore_table = null; warning_regions_table = null; + reporting_disabled = false; + } + + public static void DisableReporting () + { + if (error_stack == null) + error_stack = new Stack (); + error_stack.Push (Errors); + + if (Warnings > 0) { + if (warning_stack == null) + warning_stack = new Stack (); + warning_stack.Push (Warnings); + } + + reporting_disabled = true; + } + + public static void EnableReporting () + { + if (warning_stack != null) + Warnings = (int) warning_stack.Pop (); + + Errors = (int) error_stack.Pop (); + if (error_stack.Count == 0) { + reporting_disabled = false; + } + } + + public static IMessageRecorder msg_recorder; + + public static IMessageRecorder SetMessageRecorder (IMessageRecorder recorder) + { + IMessageRecorder previous = msg_recorder; + msg_recorder = recorder; + return previous; + } + + public interface IMessageRecorder + { + void EndSession (); + void AddMessage (AbstractMessage msg); + bool PrintMessages (); + } + + // + // Default message recorder, it uses two types of message groups. + // Common messages: messages reported in all sessions. + // Merged messages: union of all messages in all sessions. + // + public struct MessageRecorder : IMessageRecorder + { + ArrayList session_messages; + // + // A collection of exactly same messages reported in all sessions + // + ArrayList common_messages; + + // + // A collection of unique messages reported in all sessions + // + ArrayList merged_messages; + + public void EndSession () + { + if (session_messages == null) + return; + + // + // Handles the first session + // + if (common_messages == null) { + common_messages = new ArrayList (session_messages); + merged_messages = session_messages; + session_messages = null; + return; + } + + // + // Store common messages if any + // + for (int i = 0; i < common_messages.Count; ++i) { + AbstractMessage cmsg = (AbstractMessage) common_messages [i]; + bool common_msg_found = false; + foreach (AbstractMessage msg in session_messages) { + if (cmsg.Equals (msg)) { + common_msg_found = true; + break; + } + } + + if (!common_msg_found) + common_messages.RemoveAt (i); + } + + // + // Merge session and previous messages + // + for (int i = 0; i < session_messages.Count; ++i) { + AbstractMessage msg = (AbstractMessage) session_messages [i]; + bool msg_found = false; + for (int ii = 0; ii < merged_messages.Count; ++ii) { + if (msg.Equals (merged_messages [ii])) { + msg_found = true; + break; + } + } + + if (!msg_found) + merged_messages.Add (msg); + } + } + + public void AddMessage (AbstractMessage msg) + { + if (session_messages == null) + session_messages = new ArrayList (); + + session_messages.Add (msg); + } + + // + // Prints collected messages, common messages have a priority + // + public bool PrintMessages () + { + ArrayList messages_to_print = merged_messages; + if (common_messages != null && common_messages.Count > 0) { + messages_to_print = common_messages; + } + + if (messages_to_print == null) + return false; + + foreach (AbstractMessage msg in messages_to_print) + msg.Print (); + + return true; + } } + + public abstract class AbstractMessage + { + readonly string[] extra_info; + protected readonly int code; + protected readonly Location location; + readonly string message; - abstract class AbstractMessage { + protected AbstractMessage (int code, Location loc, string msg, ArrayList extraInfo) + { + this.code = code; + if (code < 0) + this.code = 8000 - code; + + this.location = loc; + this.message = msg; + if (extraInfo.Count != 0) { + this.extra_info = (string[])extraInfo.ToArray (typeof (string)); + } + } + + protected AbstractMessage (AbstractMessage aMsg) + { + this.code = aMsg.code; + this.location = aMsg.location; + this.message = aMsg.message; + this.extra_info = aMsg.extra_info; + } static void Check (int code) { @@ -113,50 +293,80 @@ namespace Mono.CSharp { } } + public override bool Equals (object obj) + { + AbstractMessage msg = obj as AbstractMessage; + if (msg == null) + return false; + + return code == msg.code && location.Equals (msg.location) && message == msg.message; + } + + public override int GetHashCode () + { + return code.GetHashCode (); + } + public abstract bool IsWarning { get; } public abstract string MessageType { get; } - public virtual void Print (int code, string location, string text) + public virtual void Print () { - if (code < 0) - code = 8000-code; + if (msg_recorder != null) { + // + // This line is useful when debugging messages recorder + // + // Console.WriteLine ("RECORDING: {0} {1} {2}", code, location, message); + msg_recorder.AddMessage (this); + return; + } + + if (reporting_disabled) + return; StringBuilder msg = new StringBuilder (); - if (location.Length != 0) { - msg.Append (location); - msg.Append (' '); + if (!location.IsNull) { + msg.Append (location.ToString ()); + msg.Append (" "); } - msg.AppendFormat ("{0} CS{1:0000}: {2}", MessageType, code, text); - Stderr.WriteLine (msg.ToString ()); + msg.AppendFormat ("{0} CS{1:0000}: {2}", MessageType, code, message); - foreach (string s in extra_information) - Stderr.WriteLine (s + MessageType); + // + // + if (Stderr == Console.Error) + Stderr.WriteLine (ColorFormat (msg.ToString ())); + else + Stderr.WriteLine (msg.ToString ()); - extra_information.Clear (); + if (extra_info != null) { + foreach (string s in extra_info) + Stderr.WriteLine (s + MessageType + ")"); + } if (Stacktrace) Console.WriteLine (FriendlyStackTrace (new StackTrace (true))); if (Fatal) { if (!IsWarning || WarningsAreErrors) - throw new Exception (text); + throw new Exception (message); } Check (code); } - public virtual void Print (int code, Location location, string text) + public virtual string ColorFormat (string s) { - Print (code, location.IsNull ? "" : location.ToString (), text); + return s; } } - sealed class WarningMessage : AbstractMessage { - Location loc = Location.Null; + sealed class WarningMessage : AbstractMessage + { readonly int Level; - public WarningMessage (int level) + public WarningMessage (int code, int level, Location loc, string message, ArrayList extra_info) + : base (code, loc, message, extra_info) { Level = level; } @@ -165,9 +375,9 @@ namespace Mono.CSharp { get { return true; } } - bool IsEnabled (int code) + bool IsEnabled () { - if (RootContext.WarningLevel < Level) + if (WarningLevel < Level) return false; if (warning_ignore_table != null) { @@ -176,36 +386,28 @@ namespace Mono.CSharp { } } - if (warning_regions_table == null || loc.Equals (Location.Null)) + if (warning_regions_table == null || location.IsNull) return true; - WarningRegions regions = (WarningRegions)warning_regions_table [loc.Name]; + WarningRegions regions = (WarningRegions)warning_regions_table [location.Name]; if (regions == null) return true; - return regions.IsWarningEnabled (code, loc.Row); + return regions.IsWarningEnabled (code, location.Row); } - public override void Print(int code, string location, string text) + public override void Print () { - if (!IsEnabled (code)) { - extra_information.Clear (); + if (!IsEnabled ()) return; - } if (WarningsAreErrors) { - new ErrorMessage ().Print (code, location, text); + new ErrorMessage (this).Print (); return; } Warnings++; - base.Print (code, location, text); - } - - public override void Print(int code, Location location, string text) - { - loc = location; - base.Print (code, location, text); + base.Print (); } public override string MessageType { @@ -215,12 +417,119 @@ namespace Mono.CSharp { } } - sealed class ErrorMessage : AbstractMessage { + 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; - public override void Print(int code, string location, string text) + 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) + { + } + + public ErrorMessage (AbstractMessage aMsg) + : base (aMsg) + { + } + + public override string ColorFormat (string s) + { + if (prefix != null) + return prefix + s + postfix; + return s; + } + + public override void Print() { Errors++; - base.Print (code, location, text); + base.Print (); } public override bool IsWarning { @@ -232,12 +541,29 @@ namespace Mono.CSharp { return "error"; } } - } - public static void FeatureIsNotStandardized (Location loc, string feature) + public static void FeatureIsNotAvailable (Location loc, string feature) { - Report.Error (1644, loc, "Feature `{0}' cannot be used because it is not part of the standardized ISO C# language specification", feature); + string version; + switch (RootContext.Version) { + case LanguageVersion.ISO_1: + version = "1.0"; + break; + case LanguageVersion.ISO_2: + 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", + 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", + feature, version); } public static string FriendlyStackTrace (Exception e) @@ -307,8 +633,16 @@ namespace Mono.CSharp { static public void SymbolRelatedToPreviousError (MemberInfo mi) { + if (reporting_disabled) + return; + Type dt = TypeManager.DropGenericTypeArguments (mi.DeclaringType); - TypeContainer temp_ds = TypeManager.LookupTypeContainer (dt); + if (TypeManager.IsDelegateType (dt)) { + SymbolRelatedToPreviousError (dt); + return; + } + + DeclSpace temp_ds = TypeManager.LookupDeclSpace (dt); if (temp_ds == null) { SymbolRelatedToPreviousError (dt.Assembly.Location, TypeManager.GetFullNameSignature (mi)); } else { @@ -332,6 +666,9 @@ namespace Mono.CSharp { static public void SymbolRelatedToPreviousError (Type type) { + if (reporting_disabled) + return; + type = TypeManager.DropGenericTypeArguments (type); if (TypeManager.IsGenericParameter (type)) { @@ -354,7 +691,7 @@ namespace Mono.CSharp { static void SymbolRelatedToPreviousError (string loc, string symbol) { - extra_information.Add (String.Format ("{0}: `{1}', name of symbol related to previous ", loc, symbol)); + extra_information.Add (String.Format ("{0} (Location of the symbol related to previous ", loc)); } public static void ExtraInformation (Location loc, string msg) @@ -377,26 +714,30 @@ namespace Mono.CSharp { static public void Warning (int code, int level, Location loc, string message) { - WarningMessage w = new WarningMessage (level); - w.Print (code, loc, message); + WarningMessage w = new WarningMessage (code, level, loc, message, extra_information); + extra_information.Clear (); + w.Print (); } static public void Warning (int code, int level, Location loc, string format, string arg) { - WarningMessage w = new WarningMessage (level); - w.Print (code, loc, String.Format (format, arg)); + WarningMessage w = new WarningMessage (code, level, loc, String.Format (format, arg), extra_information); + extra_information.Clear (); + w.Print (); } static public void Warning (int code, int level, Location loc, string format, string arg1, string arg2) { - WarningMessage w = new WarningMessage (level); - w.Print (code, loc, String.Format (format, arg1, arg2)); + WarningMessage w = new WarningMessage (code, level, loc, String.Format (format, arg1, arg2), extra_information); + extra_information.Clear (); + w.Print (); } - static public void Warning (int code, int level, Location loc, string format, params string[] args) + static public void Warning (int code, int level, Location loc, string format, params object[] args) { - WarningMessage w = new WarningMessage (level); - w.Print (code, loc, String.Format (format, args)); + WarningMessage w = new WarningMessage (code, level, loc, String.Format (format, args), extra_information); + extra_information.Clear (); + w.Print (); } static public void Warning (int code, int level, string message) @@ -421,20 +762,23 @@ namespace Mono.CSharp { static public void Error (int code, Location loc, string error) { - new ErrorMessage ().Print (code, loc, error); + new ErrorMessage (code, loc, error, extra_information).Print (); + extra_information.Clear (); } static public void Error (int code, Location loc, string format, string arg) { - new ErrorMessage ().Print (code, loc, String.Format (format, arg)); + new ErrorMessage (code, loc, String.Format (format, arg), extra_information).Print (); + extra_information.Clear (); } static public void Error (int code, Location loc, string format, string arg1, string arg2) { - new ErrorMessage ().Print (code, loc, String.Format (format, arg1, arg2)); + new ErrorMessage (code, loc, String.Format (format, arg1, arg2), extra_information).Print (); + extra_information.Clear (); } - static public void Error (int code, Location loc, string format, params string[] args) + static public void Error (int code, Location loc, string format, params object[] args) { Error (code, loc, String.Format (format, args)); } @@ -475,6 +819,15 @@ namespace Mono.CSharp { return expected_error; } } + + public static int WarningLevel { + get { + return warning_level; + } + set { + warning_level = value; + } + } public static int DebugFlags = 0; @@ -621,8 +974,8 @@ namespace Mono.CSharp { } public class InternalErrorException : Exception { - public InternalErrorException (Location loc, string text, Exception e) - : base (loc + " " + text, e) + public InternalErrorException (MemberCore mc, Exception e) + : base (mc.Location + " " + mc.GetSignatureForError (), e) { } @@ -635,6 +988,15 @@ namespace Mono.CSharp { : base (message) { } + + public InternalErrorException (string message, params object[] args) + : base (String.Format (message, args)) + { } + + public InternalErrorException (Exception e, Location loc) + : base (loc.ToString (), e) + { + } } ///