2002-07-22 Peter Williams <peterw@ximian.com>
[mono.git] / mcs / mbas / report.cs
1 //
2 // report.cs: report errors and warnings.
3 //
4 // Author: Miguel de Icaza (miguel@ximian.com)
5 //
6 // (C) 2001 Ximian, Inc. (http://www.ximian.com)
7 //
8
9 //
10 // FIXME: currently our class library does not support custom number format strings
11 //
12 using System;
13 using System.Collections;
14 using System.Diagnostics;
15
16 namespace Mono.CSharp {
17
18         /// <summary>
19         ///   This class is used to report errors and warnings t te user.
20         /// </summary>
21         public class Report {
22                 /// <summary>  
23                 ///   Errors encountered so far
24                 /// </summary>
25                 static public int Errors;
26
27                 /// <summary>  
28                 ///   Warnings encountered so far
29                 /// </summary>
30                 static public int Warnings;
31
32                 /// <summary>  
33                 ///   Whether errors should be throw an exception
34                 /// </summary>
35                 static public bool Fatal;
36                 
37                 /// <summary>  
38                 ///   Whether warnings should be considered errors
39                 /// </summary>
40                 static public bool WarningsAreErrors;
41
42                 /// <summary>  
43                 ///   Whether to dump a stack trace on errors. 
44                 /// </summary>
45                 static public bool Stacktrace;
46                 
47                 //
48                 // If the 'expected' error code is reported then the
49                 // compilation succeeds.
50                 //
51                 // Used for the test suite to excercise the error codes
52                 //
53                 static int expected_error = 0;
54
55                 //
56                 // Keeps track of the warnings that we are ignoring
57                 //
58                 static Hashtable warning_ignore_table;
59                 
60                 static void Check (int code)
61                 {
62                         if (code == expected_error){
63                                 if (Fatal)
64                                         throw new Exception ();
65                                 
66                                 Environment.Exit (0);
67                         }
68                 }
69                 
70                 static public void RealError (string msg)
71                 {
72                         Errors++;
73                         Console.WriteLine (msg);
74
75                         if (Stacktrace)
76                                 Console.WriteLine (new StackTrace ().ToString ());
77                         if (Fatal)
78                                 throw new Exception (msg);
79                 }
80
81                 static public void Error (int code, Location l, string text)
82                 {
83                         string msg = String.Format (
84                                 "{0}({1}) error CS{2:0000}: {3}", l.Name, l.Row, code, text);
85 //                              "{0}({1}) error CS{2}: {3}", l.Name, l.Row, code, text);
86                         
87                         RealError (msg);
88                         Check (code);
89                 }
90
91                 static public void Warning (int code, Location l, string text)
92                 {
93                         if (warning_ignore_table != null){
94                                 if (warning_ignore_table.Contains (code))
95                                         return;
96                         }
97                         
98                         if (WarningsAreErrors)
99                                 Error (code, l, text);
100                         else {
101                                 string row;
102                                 
103                                 if (Location.IsNull (l))
104                                         row = "";
105                                 else
106                                         row = l.Row.ToString ();
107                                 
108                                 Console.WriteLine (String.Format (
109                                         "{0}({1}) warning CS{2:0000}: {3}",
110 //                                      "{0}({1}) warning CS{2}: {3}",
111                                         l.Name,  row, code, text));
112                                 Warnings++;
113                                 Check (code);
114
115                                 if (Stacktrace)
116                                         Console.WriteLine (new StackTrace ().ToString ());
117                         }
118                 }
119                 
120                 static public void Warning (int code, string text)
121                 {
122                         Warning (code, Location.Null, text);
123                 }
124
125                 static public void Error (int code, string text)
126                 {
127                         string msg = String.Format ("error CS{0:0000}: {1}", code, text);
128 //                      string msg = String.Format ("error CS{0}: {1}", code, text);
129                         
130                         RealError (msg);
131                         Check (code);
132                 }
133
134                 static public void Message (Message m)
135                 {
136                         if (m is ErrorMessage)
137                                 Error (m.code, m.text);
138                         else
139                                 Warning (m.code, m.text);
140                 }
141
142                 static public void SetIgnoreWarning (int code)
143                 {
144                         if (warning_ignore_table == null)
145                                 warning_ignore_table = new Hashtable ();
146
147                         warning_ignore_table [code] = true;
148                 }
149                 
150                 static public int ExpectedError {
151                         set {
152                                 expected_error = value;
153                         }
154                         get {
155                                 return expected_error;
156                         }
157                 }
158         }
159
160         public class Message {
161                 public int code;
162                 public string text;
163                 
164                 public Message (int code, string text)
165                 {
166                         this.code = code;
167                         this.text = text;
168                 }
169         }
170
171         public class WarningMessage : Message {
172                 public WarningMessage (int code, string text) : base (code, text)
173                 {
174                 }
175         }
176
177         public class ErrorMessage : Message {
178                 public ErrorMessage (int code, string text) : base (code, text)
179                 {
180                 }
181
182                 //
183                 // For compatibility reasons with old code.
184                 //
185                 public static void report_error (string error)
186                 {
187                         Console.Write ("ERROR: ");
188                         Console.WriteLine (error);
189                 }
190         }
191 }
192
193