2007-01-01 Miguel de Icaza <miguel@novell.com>
[mono.git] / mcs / mcs / report.cs
index 4adde10bfe732243f4dc1ef4490ae0632ae95c3a..212be8c05979a50360f1552e97e6252eed2c82dd 100644 (file)
@@ -2,19 +2,19 @@
 // report.cs: report errors and warnings.
 //
 // Author: Miguel de Icaza (miguel@ximian.com)
+//         Marek Safar (marek.safar@seznam.cz)         
 //
 // (C) 2001 Ximian, Inc. (http://www.ximian.com)
 //
 
-//
-// FIXME: currently our class library does not support custom number format strings
-//
 using System;
+using System.IO;
 using System.Text;
 using System.Collections;
 using System.Collections.Specialized;
 using System.Diagnostics;
 using System.Reflection;
+using System.Reflection.Emit;
 
 namespace Mono.CSharp {
 
@@ -46,6 +46,8 @@ namespace Mono.CSharp {
                ///   Whether to dump a stack trace on errors. 
                /// </summary>
                static public bool Stacktrace;
+
+               static public TextWriter Stderr = Console.Error;
                
                //
                // If the 'expected' error code is reported then the
@@ -58,7 +60,7 @@ namespace Mono.CSharp {
                //
                // Keeps track of the warnings that we are ignoring
                //
-               static Hashtable warning_ignore_table;
+               public static Hashtable warning_ignore_table;
 
                static Hashtable warning_regions_table;
 
@@ -67,6 +69,42 @@ namespace Mono.CSharp {
                /// </summary>
                static StringCollection extra_information = new StringCollection ();
 
+               // 
+               // IF YOU ADD A NEW WARNING YOU HAVE TO ADD ITS ID HERE
+               //
+               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, 278, 282,
+                       419, 420, 429, 436, 440, 465, 467, 469,
+                       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,
+                       1717, 1718, 1720,
+                       1901,
+                       2002, 2023,
+                       3005, 3012, 3018, 3019, 3021, 3022, 3023, 3026, 3027,
+#if GMCS_SOURCE
+                       402, 414, 693, 1058, 1700, 3024
+#endif
+               };
+
+               static Report ()
+               {
+                       // Just to be sure that binary search is working
+                       Array.Sort (AllWarnings);
+               }
+
+               public static void Reset ()
+               {
+                       Errors = Warnings = 0;
+                       WarningsAreErrors = false;
+                       warning_ignore_table = null;
+                       warning_regions_table = null;
+               }
+
                abstract class AbstractMessage {
 
                        static void Check (int code)
@@ -91,10 +129,10 @@ namespace Mono.CSharp {
                                        msg.Append (' ');
                                }
                                msg.AppendFormat ("{0} CS{1:0000}: {2}", MessageType, code, text);
-                               Console.Error.WriteLine (msg.ToString ());
+                               Stderr.WriteLine (msg.ToString ());
 
                                foreach (string s in extra_information) 
-                                       Console.Error.WriteLine (s + MessageType);
+                                       Stderr.WriteLine (s + MessageType + ")");
 
                                extra_information.Clear ();
 
@@ -111,21 +149,14 @@ namespace Mono.CSharp {
 
                        public virtual void Print (int code, Location location, string text)
                        {
-                               if (location.Equals (Location.Null)) {
-                                       Print (code, "", text);
-                                       return;
-                               }
-                               Print (code, String.Format ("{0}({1})", location.Name, location.Row), text);
+                               Print (code, location.IsNull ? "" : location.ToString (), text);
                        }
                }
 
-               sealed class WarningMessage: AbstractMessage {
+               sealed class WarningMessage : AbstractMessage {
                        Location loc = Location.Null;
                        readonly int Level;
 
-                       public WarningMessage ():
-                               this (-1) {}
-
                        public WarningMessage (int level)
                        {
                                Level = level;
@@ -150,6 +181,9 @@ namespace Mono.CSharp {
                                        return true;
 
                                WarningRegions regions = (WarningRegions)warning_regions_table [loc.Name];
+                               if (regions == null)
+                                       return true;
+
                                return regions.IsWarningEnabled (code, loc.Row);
                        }
 
@@ -182,7 +216,7 @@ namespace Mono.CSharp {
                        }
                }
 
-               sealed class ErrorMessage: AbstractMessage {
+               sealed class ErrorMessage : AbstractMessage {
 
                        public override void Print(int code, string location, string text)
                        {
@@ -204,7 +238,7 @@ namespace Mono.CSharp {
 
                public static void FeatureIsNotStandardized (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);
+                       Report.Error (1644, loc, "Feature `{0}' cannot be used because it is not part of the standardized ISO C# language specification", feature);
                }
                
                public static string FriendlyStackTrace (Exception e)
@@ -248,31 +282,19 @@ namespace Mono.CSharp {
                        return sb.ToString ();
                }
 
-               // !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
-               // IF YOU ADD A NEW WARNING YOU HAVE TO DUPLICATE ITS ID HERE
-               //
-               public static bool IsValidWarning (int code)
+               public static void StackTrace ()
                {
-                       int[] all_warnings = new int[] {
-                               28, 67, 78, 105, 108, 109, 114, 192, 168, 169, 183, 184, 219, 251, 612, 618, 626, 628, 642, 649,
-                               659, 660, 661, 672, 1030, 1522, 1616, 1691, 1692, 1901, 2002, 2023, 3012, 3019, 8024, 8028
-                       };
-                       
-                       foreach (int i in all_warnings) {
-                               if (i == code)
-                                       return true;
-                       }
-                       return false;
+                       Console.WriteLine (FriendlyStackTrace (new StackTrace (true)));
                }
-               
-               static public void LocationOfPreviousError (Location loc)
-               {
-                       Console.Error.WriteLine (String.Format ("{0}({1}) (Location of symbol related to previous error)", loc.Name, loc.Row));
-               }    
-        
+
+               public static bool IsValidWarning (int code)
+               {       
+                       return Array.BinarySearch (AllWarnings, code) >= 0;
+               }
+                       
                static public void RuntimeMissingSupport (Location loc, string feature) 
                {
-                       Report.Error (-88, loc, "Your .NET Runtime does not support '{0}'. Please use the latest Mono runtime instead.", feature);
+                       Report.Error (-88, loc, "Your .NET Runtime does not support `{0}'. Please use the latest Mono runtime instead.", feature);
                }
 
                /// <summary>
@@ -281,23 +303,25 @@ namespace Mono.CSharp {
                /// </summary>
                static public void SymbolRelatedToPreviousError (Location loc, string symbol)
                {
-                       SymbolRelatedToPreviousError (String.Format ("{0}({1})", loc.Name, loc.Row), symbol);
+                       SymbolRelatedToPreviousError (loc.ToString (), symbol);
                }
 
                static public void SymbolRelatedToPreviousError (MemberInfo mi)
                {
-                       TypeContainer temp_ds = TypeManager.LookupTypeContainer (mi.DeclaringType);
+                       Type dt = TypeManager.DropGenericTypeArguments (mi.DeclaringType);
+                       DeclSpace temp_ds = TypeManager.LookupDeclSpace (dt);
                        if (temp_ds == null) {
-                               SymbolRelatedToPreviousError (mi.DeclaringType.Assembly.Location, TypeManager.GetFullNameSignature (mi));
+                               SymbolRelatedToPreviousError (dt.Assembly.Location, TypeManager.GetFullNameSignature (mi));
                        } else {
-                               if (mi is MethodBase) {
-                                       IMethodData md = TypeManager.GetMethod ((MethodBase)mi);
-                                       SymbolRelatedToPreviousError (md.Location, md.GetSignatureForError (temp_ds));
+                               MethodBase mb = mi as MethodBase;
+                               if (mb != null) {
+                                       mb = TypeManager.DropGenericMethodArguments (mb);
+                                       IMethodData md = TypeManager.GetMethod (mb);
+                                       SymbolRelatedToPreviousError (md.Location, md.GetSignatureForError ());
                                        return;
                                }
 
-                               string name = String.Concat (temp_ds.Name, ".", mi.Name);
-                               MemberCore mc = temp_ds.GetDefinition (name);
+                               MemberCore mc = temp_ds.GetDefinition (mi.Name);
                                SymbolRelatedToPreviousError (mc);
                        }
                }
@@ -309,21 +333,34 @@ namespace Mono.CSharp {
 
                static public void SymbolRelatedToPreviousError (Type type)
                {
-                       DeclSpace temp_ds = TypeManager.LookupDeclSpace (type);
-                       if (temp_ds == null)
-                               SymbolRelatedToPreviousError (type.Assembly.Location, TypeManager.CSharpName (type));
-                       else 
+                       type = TypeManager.DropGenericTypeArguments (type);
+
+                       if (TypeManager.IsGenericParameter (type)) {
+                               TypeParameter tp = TypeManager.LookupTypeParameter (type);
+                               if (tp != null) {
+                                       SymbolRelatedToPreviousError (tp.Location, "");
+                                       return;
+                               }
+                       }
+
+                       if (type is TypeBuilder) {
+                               DeclSpace temp_ds = TypeManager.LookupDeclSpace (type);
                                SymbolRelatedToPreviousError (temp_ds.Location, TypeManager.CSharpName (type));
+                       } else if (type.HasElementType) {
+                               SymbolRelatedToPreviousError (type.GetElementType ());
+                       } else {
+                               SymbolRelatedToPreviousError (type.Assembly.Location, TypeManager.CSharpName (type));
+                       }
                }
 
                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)
                {
-                       extra_information.Add (String.Format ("{0}({1}) {2}", loc.Name, loc.Row, msg));
+                       extra_information.Add (String.Format ("{0} {1}", loc, msg));
                }
 
                public static WarningRegions RegisterWarningRegion (Location location)
@@ -339,40 +376,88 @@ namespace Mono.CSharp {
                        return regions;
                }
 
-               static public void Warning (int code, int level, Location loc, string format, params object[] args)
+               static public void Warning (int code, int level, Location loc, string message)
                {
                        WarningMessage w = new WarningMessage (level);
-                       w.Print (code, loc, String.Format (format, args));
+                       w.Print (code, loc, message);
                }
 
-               static public void Warning (int code, Location loc, string format, params object[] args)
+               static public void Warning (int code, int level, Location loc, string format, string arg)
                {
-                       WarningMessage w = new WarningMessage ();
+                       WarningMessage w = new WarningMessage (level);
+                       w.Print (code, loc, String.Format (format, arg));
+               }
+
+               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));
+               }
+
+               static public void Warning (int code, int level, Location loc, string format, params string[] args)
+               {
+                       WarningMessage w = new WarningMessage (level);
                        w.Print (code, loc, String.Format (format, args));
                }
 
-               static public void Warning (int code, string format, params object[] args)
+               static public void Warning (int code, int level, string message)
                {
-                       Warning (code, Location.Null, String.Format (format, args));
+                       Warning (code, level, Location.Null, message);
                }
 
-               /// <summary>
-               /// Did you test your WarningLevel, that you use this method
-               /// </summary>
-               static public void Warning (int code, string text)
+               static public void Warning (int code, int level, string format, string arg)
                {
-                       Warning (code, Location.Null, text);
+                       Warning (code, level, Location.Null, format, arg);
                }
 
-               static public void Error (int code, string format, params object[] args)
+               static public void Warning (int code, int level, string format, string arg1, string arg2)
                {
-                       Error (code, Location.Null, String.Format (format, args));
+                       Warning (code, level, Location.Null, format, arg1, arg2);
+               }
+
+               static public void Warning (int code, int level, string format, params string[] args)
+               {
+                       Warning (code, level, Location.Null, String.Format (format, args));
+               }
+
+               static public void Error (int code, Location loc, string error)
+               {
+                       new ErrorMessage ().Print (code, loc, error);
+               }
+
+               static public void Error (int code, Location loc, string format, string arg)
+               {
+                       new ErrorMessage ().Print (code, loc, String.Format (format, arg));
+               }
+
+               static public void Error (int code, Location loc, string format, string arg1, string arg2)
+               {
+                       new ErrorMessage ().Print (code, loc, String.Format (format, arg1, arg2));
+               }
+
+               static public void Error (int code, Location loc, string format, params string[] args)
+               {
+                       Error (code, loc, String.Format (format, args));
+               }
+
+               static public void Error (int code, string error)
+               {
+                       Error (code, Location.Null, error);
+               }
+
+               static public void Error (int code, string format, string arg)
+               {
+                       Error (code, Location.Null, format, arg);
                }
 
-               static public void Error (int code, Location loc, string format, params object[] args)
+               static public void Error (int code, string format, string arg1, string arg2)
                {
-                       ErrorMessage e = new ErrorMessage ();
-                       e.Print (code, loc, String.Format (format, args));
+                       Error (code, Location.Null, format, arg1, arg2);
+               }
+
+               static public void Error (int code, string format, params string[] args)
+               {
+                       Error (code, Location.Null, String.Format (format, args));
                }
 
                static public void SetIgnoreWarning (int code)
@@ -537,6 +622,11 @@ namespace Mono.CSharp {
        }
 
        public class InternalErrorException : Exception {
+               public InternalErrorException (Location loc, string text, Exception e)
+                       : base (loc + " " + text, e)
+               {
+               }
+
                public InternalErrorException ()
                        : base ("Internal error")
                {
@@ -546,6 +636,10 @@ namespace Mono.CSharp {
                        : base (message)
                {
                }
+
+               public InternalErrorException (string message, params object[] args)
+                       : base (String.Format (message, args))
+               { }
        }
 
        /// <summary>
@@ -565,7 +659,7 @@ namespace Mono.CSharp {
                        public abstract bool IsEnabled (int code, bool previous);
                }
                
-               class Disable: PragmaCmd
+               class Disable : PragmaCmd
                {
                        int code;
                        public Disable (int line, int code)
@@ -580,7 +674,7 @@ namespace Mono.CSharp {
                        }
                }
 
-               class DisableAll: PragmaCmd
+               class DisableAll : PragmaCmd
                {
                        public DisableAll (int line)
                                : base (line) {}
@@ -591,7 +685,7 @@ namespace Mono.CSharp {
                        }
                }
 
-               class Enable: PragmaCmd
+               class Enable : PragmaCmd
                {
                        int code;
                        public Enable (int line, int code)
@@ -606,7 +700,7 @@ namespace Mono.CSharp {
                        }
                }
 
-               class EnableAll: PragmaCmd
+               class EnableAll : PragmaCmd
                {
                        public EnableAll (int line)
                                : base (line) {}
@@ -654,12 +748,12 @@ namespace Mono.CSharp {
                        return result;
                }
 
-               bool CheckWarningCode (int code, Location loc)
+               static bool CheckWarningCode (int code, Location loc)
                {
                        if (Report.IsValidWarning (code))
                                return true;
 
-                       Report.Warning (1691, 1, loc, "'{0}' is not a valid warning number", code);
+                       Report.Warning (1691, 1, loc, "`{0}' is not a valid warning number", code.ToString ());
                        return false;
                }
        }