2004-09-15 Marek Safar <marek.safar@seznam.cz>
[mono.git] / mcs / mcs / report.cs
index bc8e9cd84f9b215ee438e68987861754d608e1d3..388bb741f670b45c007594036cf34be715b3d995 100644 (file)
@@ -60,69 +60,139 @@ namespace Mono.CSharp {
                //
                static Hashtable warning_ignore_table;
 
+               static Hashtable warning_regions_table;
+
                /// <summary>
                /// List of symbols related to reported error/warning. You have to fill it before error/warning is reported.
                /// </summary>
                static StringCollection related_symbols = new StringCollection ();
-               
-               struct WarningData {
-                       public WarningData (int level, string text) {
-                               Level = level;
-                               Message = text;
+
+               abstract class AbstractMessage {
+
+                       static void Check (int code)
+                       {
+                               if (code == expected_error) {
+                                       Environment.Exit (0);
+                               }
                        }
 
-                       public bool IsEnabled ()
+                       public abstract string MessageType { get; }
+
+                       public virtual void Print (int code, string location, string text)
                        {
-                               return RootContext.WarningLevel >= Level;
+                               if (code < 0)
+                                       code = 8000-code;
+
+                               StringBuilder msg = new StringBuilder ();
+                               if (location.Length != 0) {
+                                       msg.Append (location);
+                                       msg.Append (' ');
+                               }
+                               msg.AppendFormat ("{0} CS{1:0000}: {2}", MessageType, code, text);
+                               Console.WriteLine (msg.ToString ());
+
+                               foreach (string s in related_symbols) {
+                                       Console.WriteLine (String.Concat (s, MessageType, ')'));
+                               }
+                               related_symbols.Clear ();
+
+                               if (Stacktrace)
+                                       Console.WriteLine (FriendlyStackTrace (new StackTrace (true)));
+
+                               if (Fatal)
+                                       throw new Exception (text);
+
+                               Check (code);
                        }
 
-                       public string Format (params object[] args)
+                       public virtual void Print (int code, Location location, string text)
                        {
-                               return String.Format (Message, args);
+                               if (location.Equals (Location.Null)) {
+                                       Print (code, "", text);
+                                       return;
+                               }
+                               Print (code, String.Format ("{0}({1})", location.Name, location.Row), text);
                        }
+               }
 
-                       readonly string Message;
+               sealed class WarningMessage: AbstractMessage {
+                       Location loc = Location.Null;
                        readonly int Level;
-               }
 
-               static string GetErrorMsg (int error_no)
-               {
-                       switch (error_no) {
-                               case 3001: return "Argument type '{0}' is not CLS-compliant";
-                               case 3002: return "Return type of '{0}' is not CLS-compliant";
-                               case 3003: return "Type of '{0}' is not CLS-compliant";
-                               case 3005: return "Identifier '{0}' differing only in case is not CLS-compliant";
-                               case 3006: return "Overloaded method '{0}' differing only in ref or out, or in array rank, is not CLS-compliant";
-                               case 3008: return "Identifier '{0}' is not CLS-compliant";
-                               case 3009: return "'{0}': base type '{1}' is not CLS-compliant";
-                               case 3010: return "'{0}': CLS-compliant interfaces must have only CLS-compliant members";
-                               case 3011: return "'{0}': only CLS-compliant members can be abstract";
-                               case 3013: return "Added modules must be marked with the CLSCompliant attribute to match the assembly";
-                               case 3014: return "'{0}' cannot be marked as CLS-compliant because the assembly does not have a CLSCompliant attribute";
-                               case 3015: return "'{0}' has no accessible constructors which use only CLS-compliant types";
-                               case 3016: return "Arrays as attribute arguments are not CLS-compliant";
+                       public WarningMessage ():
+                               this (-1) {}
+
+                       public WarningMessage (int level)
+                       {
+                               Level = level;
+                       }
+
+                       bool IsEnabled (int code)
+                       {
+                               if (RootContext.WarningLevel < Level)
+                                       return false;
+
+                               if (warning_ignore_table != null) {
+                                       if (warning_ignore_table.Contains (code)) {
+                                               return false;
+                                       }
+                               }
+
+                               if (warning_regions_table == null || loc.Equals (Location.Null))
+                                       return true;
+
+                               WarningRegions regions = (WarningRegions)warning_regions_table [loc.Name];
+                               return regions.IsWarningEnabled (code, loc.Row);
+                       }
+
+                       public override void Print(int code, string location, string text)
+                       {
+                               if (!IsEnabled (code)) {
+                                       related_symbols.Clear ();
+                                       return;
+                               }
+
+                               if (WarningsAreErrors) {
+                                       new ErrorMessage ().Print (code, location, text);
+                                       return;
+                               }
+
+                               Warnings++;
+                               base.Print (code, location, text);
+                       }
+
+                       public override void Print(int code, Location location, string text)
+                       {
+                               loc = location;
+                               base.Print (code, location, text);
+                       }
+
+                       public override string MessageType {
+                               get {
+                                       return "warning";
+                               }
                        }
-                       throw new InternalErrorException (String.Format ("Missing error '{0}' text", error_no));
                }
 
-               static WarningData GetWarningMsg (int warn_no)
-               {
-                       switch (warn_no) {
-                               case 3012: return new WarningData (1, "You must specify the CLSCompliant attribute on the assembly, not the module, to enable CLS compliance checking");
-                               case 3019: return new WarningData (2, "CLS compliance checking will not be performed on '{0}' because it is private or internal");
+               sealed class ErrorMessage: AbstractMessage {
+
+                       public override void Print(int code, string location, string text)
+                       {
+                               Errors++;
+                               base.Print (code, location, text);
+                       }
+
+                       public override string MessageType {
+                               get {
+                                       return "error";
+                               }
                        }
 
-                       throw new InternalErrorException (String.Format ("Wrong warning number '{0}'", warn_no));
                }
-               
-               static void Check (int code)
+
+               public static void FeatureIsNotStandardized (Location loc, string feature)
                {
-                       if (code == expected_error){
-                               if (Fatal)
-                                       throw new Exception ();
-                               
-                               Environment.Exit (0);
-                       }
+                       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)
@@ -165,16 +235,34 @@ 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)
+               {
+                       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;
+               }
                
-               [Obsolete ("Use SymbolRelatedToPreviousError for better error description")]
                static public void LocationOfPreviousError (Location loc)
                {
                        Console.WriteLine (String.Format ("{0}({1}) (Location of symbol related to previous error)", loc.Name, loc.Row));
-               }                
+               }    
+        
+               static public void RuntimeMissingSupport (string feature) 
+               {
+                       Report.Error (-88, "Your .NET Runtime does not support '{0}'. Please use the latest Mono runtime instead.", feature);
+               }
 
                /// <summary>
-                /// In most error cases is very useful to have information about symbol that caused the error.
-                /// Call this method before you call Report.Error when it makes sense.
+               /// In most error cases is very useful to have information about symbol that caused the error.
+               /// Call this method before you call Report.Error when it makes sense.
                /// </summary>
                static public void SymbolRelatedToPreviousError (Location loc, string symbol)
                {
@@ -183,181 +271,88 @@ namespace Mono.CSharp {
 
                static public void SymbolRelatedToPreviousError (MemberInfo mi)
                {
-                       DeclSpace temp_ds = TypeManager.LookupDeclSpace (mi.DeclaringType);
+                       TypeContainer temp_ds = TypeManager.LookupTypeContainer (mi.DeclaringType);
                        if (temp_ds == null) {
                                SymbolRelatedToPreviousError (mi.DeclaringType.Assembly.Location, TypeManager.GetFullNameSignature (mi));
                        } else {
+                               if (mi is MethodBase) {
+                                       IMethodData md = TypeManager.GetMethod ((MethodBase)mi);
+                                       SymbolRelatedToPreviousError (md.Location, md.GetSignatureForError (temp_ds));
+                                       return;
+                               }
+
                                string name = String.Concat (temp_ds.Name, ".", mi.Name);
-                               MemberCore mc = temp_ds.GetDefinition (name) as MemberCore;
-                               SymbolRelatedToPreviousError (mc.Location, mc.GetSignatureForError ());
+                               MemberCore mc = temp_ds.GetDefinition (name);
+                               SymbolRelatedToPreviousError (mc);
                        }
                }
 
-               static public void SymbolRelatedToPreviousError (Type type)
+               static public void SymbolRelatedToPreviousError (MemberCore mc)
                {
-                       SymbolRelatedToPreviousError (type.Assembly.Location, TypeManager.CSharpName (type));
+                       SymbolRelatedToPreviousError (mc.Location, mc.GetSignatureForError ());
                }
 
-               static void SymbolRelatedToPreviousError (string loc, string symbol)
+               static public void SymbolRelatedToPreviousError (Type type)
                {
-                       related_symbols.Add (String.Format ("{0}: ('{1}' name of symbol related to previous error)", loc, symbol));
+                       DeclSpace temp_ds = TypeManager.LookupDeclSpace (type);
+                       if (temp_ds == null)
+                               SymbolRelatedToPreviousError (type.Assembly.Location, TypeManager.CSharpName (type));
+                       else 
+                               SymbolRelatedToPreviousError (temp_ds.Location, TypeManager.CSharpName (type));
                }
 
-               static public void RealError (string msg)
+               static void SymbolRelatedToPreviousError (string loc, string symbol)
                {
-                       Errors++;
-                       Console.WriteLine (msg);
-
-                       foreach (string s in related_symbols)
-                               Console.WriteLine (s);
-                       related_symbols.Clear ();
-
-                       if (Stacktrace)
-                               Console.WriteLine (FriendlyStackTrace (new StackTrace (true)));
-                       
-                       if (Fatal)
-                               throw new Exception (msg);
+                       related_symbols.Add (String.Format ("{0}: '{1}' (name of symbol related to previous ", loc, symbol));
                }
 
-
-               /// <summary>
-               /// Method reports warning message. Only one reason why exist Warning and Report methods is beter code readability.
-               /// </summary>
-               static public void Warning_T (int code, Location loc, params object[] args)
+               public static WarningRegions RegisterWarningRegion (Location location)
                {
-                       WarningData warning = GetWarningMsg (code);
-                       if (warning.IsEnabled ())
-                               Warning (code, loc, warning.Format (args));
-
-                       related_symbols.Clear ();
-               }
+                       if (warning_regions_table == null)
+                               warning_regions_table = new Hashtable ();
 
-               /// <summary>
-               /// Reports error message.
-               /// </summary>
-               static public void Error_T (int code, Location loc, params object[] args)
-               {
-                       Error_T (code, String.Format ("{0}({1})", loc.Name, loc.Row), args);
+                       WarningRegions regions = (WarningRegions)warning_regions_table [location.Name];
+                       if (regions == null) {
+                               regions = new WarningRegions ();
+                               warning_regions_table.Add (location.Name, regions);
+                       }
+                       return regions;
                }
 
-               static public void Error_T (int code, string location, params object[] args)
+               static public void Warning (int code, int level, Location loc, string format, params object[] args)
                {
-                       string errorText = String.Format (GetErrorMsg (code), args);
-                       PrintError (code, location, errorText);
+                       WarningMessage w = new WarningMessage (level);
+                       w.Print (code, loc, String.Format (format, args));
                }
 
-               static void PrintError (int code, string l, string text)
+               static public void Warning (int code, Location loc, string format, params object[] args)
                {
-                       if (code < 0)
-                               code = 8000-code;
-                       
-                       string msg = String.Format ("{0} error CS{1:0000}: {2}", l, code, text);
-                       RealError (msg);
-                       Check (code);
+                       WarningMessage w = new WarningMessage ();
+                       w.Print (code, loc, String.Format (format, args));
                }
 
-               static public void Error (int code, Location l, string text)
+               static public void Warning (int code, string format, params object[] args)
                {
-                       if (code < 0)
-                               code = 8000-code;
-                       
-                       string msg = String.Format (
-                               "{0}({1}) error CS{2:0000}: {3}", l.Name, l.Row, code, text);
-//                             "{0}({1}) error CS{2}: {3}", l.Name, l.Row, code, text);
-                       
-                       RealError (msg);
-                       Check (code);
+                       Warning (code, Location.Null, String.Format (format, args));
                }
 
-               static public void Warning (int code, Location l, string text)
-               {
-                       if (code < 0)
-                               code = 8000-code;
-                       
-                       if (warning_ignore_table != null){
-                               if (warning_ignore_table.Contains (code)) {
-                                       related_symbols.Clear ();
-                                       return;
-                               }
-                       }
-                       
-                       if (WarningsAreErrors)
-                               Error (code, l, text);
-                       else {
-                               string row;
-                               
-                               if (Location.IsNull (l))
-                                       row = "";
-                               else
-                                       row = l.Row.ToString ();
-                               
-                               Console.WriteLine (String.Format (
-                                       "{0}({1}) warning CS{2:0000}: {3}",
-//                                     "{0}({1}) warning CS{2}: {3}",
-                                       l.Name,  row, code, text));
-                               Warnings++;
-
-                               foreach (string s in related_symbols)
-                                       Console.WriteLine (s);
-                               related_symbols.Clear ();
-
-                               Check (code);
-
-                               if (Stacktrace)
-                                       Console.WriteLine (new StackTrace ().ToString ());
-                       }
-               }
-               
+               /// <summary>
+               /// Did you test your WarningLevel, that you use this method
+               /// </summary>
                static public void Warning (int code, string text)
                {
                        Warning (code, Location.Null, text);
                }
 
-               static public void Warning (int code, int level, string text)
-               {
-                       if (RootContext.WarningLevel >= level)
-                               Warning (code, Location.Null, text);
-               }
-
-               static public void Warning (int code, int level, Location l, string text)
+               static public void Error (int code, string format, params object[] args)
                {
-                       if (RootContext.WarningLevel >= level)
-                               Warning (code, l, text);
-               }
-
-               static public void Error (int code, string text)
-               {
-                       if (code < 0)
-                               code = 8000-code;
-                       
-                       string msg = String.Format ("error CS{0:0000}: {1}", code, text);
-//                     string msg = String.Format ("error CS{0}: {1}", code, text);
-                       
-                       RealError (msg);
-                       Check (code);
+                       Error (code, Location.Null, String.Format (format, args));
                }
 
                static public void Error (int code, Location loc, string format, params object[] args)
                {
-                       Error (code, loc, String.Format (format, args));
-               }
-
-               static public void Warning (int code, Location loc, string format, params object[] args)
-               {
-                       Warning (code, loc, String.Format (format, args));
-               }
-
-               static public void Warning (int code, string format, params object[] args)
-               {
-                       Warning (code, String.Format (format, args));
-               }
-
-               static public void Message (Message m)
-               {
-                       if (m is ErrorMessage)
-                               Error (m.code, m.text);
-                       else
-                               Warning (m.code, m.text);
+                       ErrorMessage e = new ErrorMessage ();
+                       e.Print (code, loc, String.Format (format, args));
                }
 
                static public void SetIgnoreWarning (int code)
@@ -368,14 +363,14 @@ namespace Mono.CSharp {
                        warning_ignore_table [code] = true;
                }
                
-                static public int ExpectedError {
-                        set {
-                                expected_error = value;
-                        }
-                        get {
-                                return expected_error;
-                        }
-                }
+               static public int ExpectedError {
+                       set {
+                               expected_error = value;
+                       }
+                       get {
+                               return expected_error;
+                       }
+               }
 
                public static int DebugFlags = 0;
 
@@ -435,38 +430,6 @@ namespace Mono.CSharp {
                }
        }
 
-       public class Message {
-               public int code;
-               public string text;
-               
-               public Message (int code, string text)
-               {
-                       this.code = code;
-                       this.text = text;
-               }
-       }
-
-       public class WarningMessage : Message {
-               public WarningMessage (int code, string text) : base (code, text)
-               {
-               }
-       }
-
-       public class ErrorMessage : Message {
-               public ErrorMessage (int code, string text) : base (code, text)
-               {
-               }
-
-               //
-               // For compatibility reasons with old code.
-               //
-               public static void report_error (string error)
-               {
-                       Console.Write ("ERROR: ");
-                       Console.WriteLine (error);
-               }
-       }
-
        public enum TimerType {
                FindMembers     = 0,
                TcFindMembers   = 1,
@@ -564,4 +527,120 @@ namespace Mono.CSharp {
                {
                }
        }
+
+       /// <summary>
+       /// Handles #pragma warning
+       /// </summary>
+       public class WarningRegions {
+
+               abstract class PragmaCmd
+               {
+                       public int Line;
+
+                       protected PragmaCmd (int line)
+                       {
+                               Line = line;
+                       }
+
+                       public abstract bool IsEnabled (int code, bool previous);
+               }
+               
+               class Disable: PragmaCmd
+               {
+                       int code;
+                       public Disable (int line, int code)
+                               : base (line)
+                       {
+                               this.code = code;
+                       }
+
+                       public override bool IsEnabled (int code, bool previous)
+                       {
+                               return this.code == code ? false : previous;
+                       }
+               }
+
+               class DisableAll: PragmaCmd
+               {
+                       public DisableAll (int line)
+                               : base (line) {}
+
+                       public override bool IsEnabled(int code, bool previous)
+                       {
+                               return false;
+                       }
+               }
+
+               class Enable: PragmaCmd
+               {
+                       int code;
+                       public Enable (int line, int code)
+                               : base (line)
+                       {
+                               this.code = code;
+                       }
+
+                       public override bool IsEnabled(int code, bool previous)
+                       {
+                               return this.code == code ? true : previous;
+                       }
+               }
+
+               class EnableAll: PragmaCmd
+               {
+                       public EnableAll (int line)
+                               : base (line) {}
+
+                       public override bool IsEnabled(int code, bool previous)
+                       {
+                               return true;
+                       }
+               }
+
+
+               ArrayList regions = new ArrayList ();
+
+               public void WarningDisable (int line)
+               {
+                       regions.Add (new DisableAll (line));
+               }
+
+               public void WarningDisable (Location location, int code)
+               {
+                       if (CheckWarningCode (code, location))
+                               regions.Add (new Disable (location.Row, code));
+               }
+
+               public void WarningEnable (int line)
+               {
+                       regions.Add (new EnableAll (line));
+               }
+
+               public void WarningEnable (Location location, int code)
+               {
+                       if (CheckWarningCode (code, location))
+                               regions.Add (new Enable (location.Row, code));
+               }
+
+               public bool IsWarningEnabled (int code, int src_line)
+               {
+                       bool result = true;
+                       foreach (PragmaCmd pragma in regions) {
+                               if (src_line < pragma.Line)
+                                       break;
+
+                               result = pragma.IsEnabled (code, result);
+                       }
+                       return result;
+               }
+
+               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);
+                       return false;
+               }
+       }
 }