2002-08-07 Martin Baulig <martin@gnome.org>
[mono.git] / mcs / mcs / 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.Text;
14 using System.Collections;
15 using System.Diagnostics;
16
17 namespace Mono.CSharp {
18
19         /// <summary>
20         ///   This class is used to report errors and warnings t te user.
21         /// </summary>
22         public class Report {
23                 /// <summary>  
24                 ///   Errors encountered so far
25                 /// </summary>
26                 static public int Errors;
27
28                 /// <summary>  
29                 ///   Warnings encountered so far
30                 /// </summary>
31                 static public int Warnings;
32
33                 /// <summary>  
34                 ///   Whether errors should be throw an exception
35                 /// </summary>
36                 static public bool Fatal;
37                 
38                 /// <summary>  
39                 ///   Whether warnings should be considered errors
40                 /// </summary>
41                 static public bool WarningsAreErrors;
42
43                 /// <summary>  
44                 ///   Whether to dump a stack trace on errors. 
45                 /// </summary>
46                 static public bool Stacktrace;
47                 
48                 //
49                 // If the 'expected' error code is reported then the
50                 // compilation succeeds.
51                 //
52                 // Used for the test suite to excercise the error codes
53                 //
54                 static int expected_error = 0;
55
56                 //
57                 // Keeps track of the warnings that we are ignoring
58                 //
59                 static Hashtable warning_ignore_table;
60                 
61                 static void Check (int code)
62                 {
63                         if (code == expected_error){
64                                 if (Fatal)
65                                         throw new Exception ();
66                                 
67                                 Environment.Exit (0);
68                         }
69                 }
70                 
71                 static public void RealError (string msg)
72                 {
73                         Errors++;
74                         Console.WriteLine (msg);
75
76                         if (Stacktrace)
77                                 Console.WriteLine (new StackTrace ().ToString ());
78                         if (Fatal)
79                                 throw new Exception (msg);
80                 }
81
82                 static public void Error (int code, Location l, string text)
83                 {
84                         string msg = String.Format (
85                                 "{0}({1}) error CS{2:0000}: {3}", l.Name, l.Row, code, text);
86 //                              "{0}({1}) error CS{2}: {3}", l.Name, l.Row, code, text);
87                         
88                         RealError (msg);
89                         Check (code);
90                 }
91
92                 static public void Warning (int code, Location l, string text)
93                 {
94                         if (warning_ignore_table != null){
95                                 if (warning_ignore_table.Contains (code))
96                                         return;
97                         }
98                         
99                         if (WarningsAreErrors)
100                                 Error (code, l, text);
101                         else {
102                                 string row;
103                                 
104                                 if (Location.IsNull (l))
105                                         row = "";
106                                 else
107                                         row = l.Row.ToString ();
108                                 
109                                 Console.WriteLine (String.Format (
110                                         "{0}({1}) warning CS{2:0000}: {3}",
111 //                                      "{0}({1}) warning CS{2}: {3}",
112                                         l.Name,  row, code, text));
113                                 Warnings++;
114                                 Check (code);
115
116                                 if (Stacktrace)
117                                         Console.WriteLine (new StackTrace ().ToString ());
118                         }
119                 }
120                 
121                 static public void Warning (int code, string text)
122                 {
123                         Warning (code, Location.Null, text);
124                 }
125
126                 static public void Error (int code, string text)
127                 {
128                         string msg = String.Format ("error CS{0:0000}: {1}", code, text);
129 //                      string msg = String.Format ("error CS{0}: {1}", code, text);
130                         
131                         RealError (msg);
132                         Check (code);
133                 }
134
135                 static public void Message (Message m)
136                 {
137                         if (m is ErrorMessage)
138                                 Error (m.code, m.text);
139                         else
140                                 Warning (m.code, m.text);
141                 }
142
143                 static public void SetIgnoreWarning (int code)
144                 {
145                         if (warning_ignore_table == null)
146                                 warning_ignore_table = new Hashtable ();
147
148                         warning_ignore_table [code] = true;
149                 }
150                 
151                 static public int ExpectedError {
152                         set {
153                                 expected_error = value;
154                         }
155                         get {
156                                 return expected_error;
157                         }
158                 }
159
160                 public static int DebugFlags = 0;
161
162                 [Conditional ("MCS_DEBUG")]
163                 static public void Debug (string message, params object[] args)
164                 {
165                         Debug (4, message, args);
166                 }
167                         
168                 [Conditional ("MCS_DEBUG")]
169                 static public void Debug (int category, string message, params object[] args)
170                 {
171                         if ((category & DebugFlags) == 0)
172                                 return;
173
174                         StringBuilder sb = new StringBuilder (message);
175
176                         if ((args != null) && (args.Length > 0)) {
177                                 sb.Append (": ");
178
179                                 bool first = true;
180                                 foreach (object arg in args) {
181                                         if (first)
182                                                 first = false;
183                                         else
184                                                 sb.Append (",");
185                                         if (arg == null)
186                                                 sb.Append ("null");
187                                         else if (arg is ICollection)
188                                                 sb.Append (PrintCollection ((ICollection) arg));
189                                         else
190                                                 sb.Append (arg);
191                                 }
192                         }
193
194                         Console.WriteLine (sb.ToString ());
195                 }
196
197                 static public string PrintCollection (ICollection collection)
198                 {
199                         StringBuilder sb = new StringBuilder ();
200
201                         sb.Append (collection.GetType ());
202                         sb.Append ("(");
203
204                         bool first = true;
205                         foreach (object o in collection) {
206                                 if (first)
207                                         first = false;
208                                 else
209                                         sb.Append (",");
210                                 sb.Append (o);
211                         }
212
213                         sb.Append (")");
214                         return sb.ToString ();
215                 }
216         }
217
218         public class Message {
219                 public int code;
220                 public string text;
221                 
222                 public Message (int code, string text)
223                 {
224                         this.code = code;
225                         this.text = text;
226                 }
227         }
228
229         public class WarningMessage : Message {
230                 public WarningMessage (int code, string text) : base (code, text)
231                 {
232                 }
233         }
234
235         public class ErrorMessage : Message {
236                 public ErrorMessage (int code, string text) : base (code, text)
237                 {
238                 }
239
240                 //
241                 // For compatibility reasons with old code.
242                 //
243                 public static void report_error (string error)
244                 {
245                         Console.Write ("ERROR: ");
246                         Console.WriteLine (error);
247                 }
248         }
249 }
250
251